
Building a Linked List and Pointer Manipulation
Learn the fundamentals of building a linked list in C++, including dynamic memory allocation, pointer manipulation, node creation, traversal, and more. Dive into the intricacies of pointer dereferencing and member selection to understand how to work with linked lists effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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; A 0x600 . .info . .link
ptr is a pointer to a node ptr A 0x600 . .info . .link ptr
*ptr is the entire node pointed to by ptr ptr A 0x600 . .info . .link *ptr
ptr->info is a node member ptr A 0x600 . .info . . link ptr->info or (*ptr).info // Equivalent
ptr->link is a node member ptr A 0x600 . . info . . link ptr->link or (*ptr).link // Equivalent
Pointer Dereferencing and Member Selection A 0x600 ptr . . info . . link ptr ptr A 0x600 . . info . . link *ptr ptr A 0x600 . . info . . link (*ptr).info or ptr->info
Building a Linked List newNodePtr head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x200 head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x200 L head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x500 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x500 0x200 I L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x500 0x200 I 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x500 0x200 0x200 L NULL head I newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x300 0x500 0x200 I 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x300 0x500 0x200 T I 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Building a Linked List newNodePtr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head newNodePtr = new NodeType; newNodePtr->info = L ; newNodePtr->link = NULL; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = I ; newNodePtr->link = head; head = newNodePtr; newNodePtr = new NodeType; newNodePtr->info = T ; newNodePtr->link = head; head = newNodePtr;
Traversing a Linked List ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x300 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x300 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x300 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: T ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x500 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x500 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x500 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: I ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x200 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: I ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x200 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: I ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List 0x200 ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: L ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List NULL ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: L ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List NULL ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: L ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Traversing a Linked List NULL ptr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL head 0x300 Output: L ptr = head; while (ptr != NULL){ cout << ptr->info; ptr = ptr->link; }
Deleting Node I prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 NULL 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 NULL 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x500 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x200 I 0x200 L NULL 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 0x300 0x300 prev curr 0x300 0x500 0x200 T 0x200 ? ? L NULL 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 0x300 NULL prev curr 0x300 0x200 T 0x200 L NULL 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 0x300 NULL prev curr 0x300 0x200 T 0x200 L NULL 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 0x300 NULL prev curr 0x300 0x200 T 0x200 0x300 0x200 L NULL L NULL head // 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