Linked Lists Introduction Building a Linked List Traversing

  • Slides: 48
Download presentation
Linked Lists Introduction Building a Linked List Traversing a Linked List Deleting a Node

Linked Lists Introduction Building a Linked List Traversing a Linked List Deleting a Node Dr. Hyrum D. Carroll (with some material from Dale & Weems’s Programming and Problem Solving with C++ slides)

Declarations for a Dynamic Linked List // Type declarations struct Node. Type { char

Declarations for a Dynamic Linked List // Type declarations struct Node. Type { char info; Node. Type* link; } // Variable DECLARATIONS Node. Type* head; Node. Type* ptr; ‘A’. info 0 x 600. link

Pointer Dereferencing and Member Selection ptr ‘A’. info 0 x 600. link ptr *ptr

Pointer Dereferencing and Member Selection ptr ‘A’. info 0 x 600. link ptr *ptr (*ptr). info or ptr->info

ptr is a pointer to a node ptr ‘A’. info ptr 0 x 600.

ptr is a pointer to a node ptr ‘A’. info ptr 0 x 600. link

*ptr is the entire node pointed to by ptr ‘A’. info *ptr 0 x

*ptr is the entire node pointed to by ptr ‘A’. info *ptr 0 x 600. link

ptr->info is a node member ptr ‘A’. info ptr->info or (*ptr). info 0 x

ptr->info is a node member ptr ‘A’. info ptr->info or (*ptr). info 0 x 600 . link // Equivalent

ptr->link is a node member ptr ‘A’ . info ptr->link or (*ptr). link 0

ptr->link is a node member ptr ‘A’ . info ptr->link or (*ptr). link 0 x 600. link // Equivalent

BUILDING A LINKED LIST

BUILDING A LINKED LIST

Building a Linked List new. Node. Ptr head new. Node. Ptr = new Node.

Building a Linked List new. Node. Ptr head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr;

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr;

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘L’

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 200 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘I’ 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘I’ 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 500 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘I’ 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 300 head new. Node. Ptr

Building a Linked List new. Node. Ptr 0 x 300 head new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr head 0 x 300 0 x 500

Building a Linked List new. Node. Ptr head 0 x 300 0 x 500 ‘T’ ‘I’ new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 300 head ‘T’ 0 x

Building a Linked List new. Node. Ptr 0 x 300 head ‘T’ 0 x 500 new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘I’ 0 x 200 ‘L’ NULL

Building a Linked List new. Node. Ptr 0 x 300 head ‘T’ 0 x

Building a Linked List new. Node. Ptr 0 x 300 head ‘T’ 0 x 500 new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘L’; new. Node. Ptr->link = NULL; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘I’; new. Node. Ptr->link = head; head = new. Node. Ptr; new. Node. Ptr = new Node. Type; new. Node. Ptr->info = ‘T’; new. Node. Ptr->link = head; head = new. Node. Ptr; ‘I’ 0 x 200 ‘L’ NULL

TRAVERSING A LINKED LIST

TRAVERSING A LINKED LIST

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ Output:

Traversing a Linked List ptr 0 x 300 head 0 x 300 ‘T’ Output: ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x 300 ‘T’ Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x 300 ‘T’ Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 500 0 x 300 head 0 x 300 ‘T’ Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x 300 ‘T’ Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x 300 ‘T’ Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x

Traversing a Linked List ptr 0 x 200 0 x 300 head 0 x 300 ‘T’ Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’ Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’ Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’

Traversing a Linked List ptr NULL 0 x 300 head 0 x 300 ‘T’ Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; } 0 x 500 ‘I’ 0 x 200 ‘L’ NULL

DELETING A NODE

DELETING A NODE

Deleting Node ’I’ prev curr 0 x 300 head 0 x 300 ‘T’ 0

Deleting Node ’I’ prev curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev NULL curr 0 x 300 head 0 x 300 ‘T’

Deleting Node ’I’ prev NULL curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev NULL curr 0 x 300 head 0 x 300 ‘T’

Deleting Node ’I’ prev NULL curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 0 x 200 ‘I’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x

Deleting Node ’I’ prev 0 x 300 curr 0 x 300 head 0 x 300 ‘T’ 0 x 500 0 x 200 ? // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0 x 300 ‘T’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0 x 300 ‘T’ 0 x 200 // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer ‘L’ NULL

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0

Deleting Node ’I’ prev 0 x 300 curr NULL 0 x 300 head 0 x 300 ‘T’ 0 x 200 ‘L’ // find node ’I’ Node. Type *curr = head, *prev = NULL; while( curr != NULL && cur->info != ’I’){ prev = curr; curr = curr->link; } if( curr == NULL){/*’I’ not found*/ return; } prev->link = curr->link; // copy pointer to prev’s delete curr; // free up memory on the heap curr = NULL; // clean up the pointer NULL