Traversing a Linked List 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 NodeType { char info; NodeType* link; }; // Variable DECLARATIONS NodeType* head; NodeType* ptr; . info . link ‘A’ 0x600
ptr is a pointer to a node . info . link ‘A’ 0x600 ptr
*ptr is the entire node pointed to by ptr ‘A’ 0x600 . info . link *ptr
ptr->info is a node member . info . link ‘A’ 0x600 ptr->info or (*ptr).info // Equivalent
ptr->link is a node member . info . link ‘A’ 0x600 ptr->link or (*ptr).link // Equivalent
Pointer Dereferencing and Member Selection ptr . info . link ‘A’ 0x600 ptr ptr . info . link ‘A’ 0x600 *ptr ptr . info . link (*ptr).info or ptr->info ‘A’ 0x600
Building a Linked List
Building a Linked List newNodePtr head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x200 head ‘L’ newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x500 0x200 head ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x500 0x200 head ‘I’ ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List newNodePtr 0x500 0x200 head ‘I’ 0x200 ‘L’ NULL newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List ‘T’ ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Building a Linked List ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL newNodePtr 0x300 0x500 0x200 head newNodePtr = new NodeType; newNodePtr->info = ‘L’; newNodePtr->link = NULL; head = newNodePtr; newNodePtr->info = ‘I’; newNodePtr->link = head; newNodePtr->info = ‘T’;
Traversing a Linked List
Traversing a Linked List ptr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘T’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x500 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘I’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr 0x200 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List ptr NULL ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 Output: ‘L’ ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Deleting a Node
Deleting Node ’I’ prev curr ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr NULL 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x500 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ‘I’ 0x200 ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 0x300 ‘T’ 0x200 ? ? ‘L’ NULL 0x300 0x500 0x200 head 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head ‘T’ 0x200 ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 NULL 0x300 0x200 head ‘T’ 0x200 ‘L’ NULL 0x300 // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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
Deleting Node ’I’ prev curr 0x300 NULL ‘T’ 0x200 ‘L’ NULL 0x300 0x200 head 0x300 0x200 ‘L’ NULL // find node ’I’ NodeType *curr = head, *prev = NULL; while( curr != NULL && curr->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