200 likes | 337 Vues
This lesson covers the essentials of dynamic linked lists, focusing on memory allocation using the new operator, traversal methods, and the deallocation process using the delete operator. Students will learn to create a dynamic linked list from scratch, allocate memory from the heap, and implement traversal functions. The lesson progresses into variations of linked lists while ensuring memory management is handled efficiently. By the end, learners will have a nuanced understanding of how dynamic memory operates within the structure of linked lists in C++.
E N D
Objectives • Memory from the heap • Dynamic memory allocation using the new operator • Build a dynamic linked list • Another way of traversing a linked list • Deallocating memory using delete operator • Dynamic linked list variation
Memory Setup Operating System Program Stack Heap
Operator new Operating System int * ptr; ptr = new int; Program Stack ptr 9ab Heap 9ab int
Program to Create a Dynamic Linked List (Part 1) struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list /*allocate the first node*/ head= new entry; temp = head; // create 4 more nodes for (inti = 0; i < 4; i++) {cin >>temp‑>value; /*put address of next cell in .next member */ temp‑>next = new entry; temp = temp‑>next; //temp now points to last node } temp‑>value = 0; /*last node points to nothing*/ temp‑>next = 0; /*it's a dummy node*/
(Part 2) Program to Create a Dynamic Linked List // traverse the list for (entry* p = head; p; p = p->next) { cout << "node address: " << p << " value " << p->value << " next " << p->next << endl; } // deallocate memory while (head) { entry* n = head->next; delete head; head = n; } return 0; }
Starting the Linked List struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list temp head 0
Create 1st Node /*allocate the first node*/ head= new entry; temp = head; (76c) temp 76c head 76c
Loop for Input and Creating Additional Nodes for (inti = 0; i < 4; i++) {cin >>temp‑>value; /*put address of next cell in .next member */ temp‑>next = new entry; temp = temp‑>next; //temp now points to last node } (76c) 76c 42 temp head 76c
Adding Additional Nodes for (inti = 0; i < 4; i++) {cin >>temp‑>value; /*put address of next cell in .next member */ temp‑>next = new entry; temp = temp‑>next; //temp now points to last node } 76c temp (76c) (88b) 42 88b head 76c
Advancing temp to Next Node for (inti = 0; i < 4; i++) {cin >>temp‑>value; /*put address of next cell in .next member */ temp‑>next = new entry; temp = temp‑>next; //temp now points to last node } 88b temp (76c) (88b) 42 88b head 76c
Repeating the Loop for (inti = 0; i < 4; i++) {cin >>temp‑>value; /*put address of next cell in .next member */ temp‑>next = new entry; temp = temp‑>next; //temp now points to last node } (76c) (88b) (4af) 97 4af 42 88b head 76c 4af temp
End of the Loop head 42 97 35 17 temp
Terminating the Linked List temp‑>value = 0; /*last node points to nothing*/ temp‑>next = 0; /*it's a dummy node*/ head 17 0 0 42 97 35 temp
Printing the Linked List // traverse the list for (entry* p = head; p; p = p->next) { cout << "node address: " << p << " value " << p->value << " next " << p->next << endl; } head 17 0 0 42 97 35 p temp
Output node address: 76c value 42 next 88b node address: 88b value 97 next 4af node address: 4af value 35 next 57d node address: 57d value 17 next a6e node address: a6e value 0 next 0 (57d) (a6e) (76c) (88b) (4af) 17 a6e 0 0 97 4af 42 88b 35 57d head 76c a6e 0 temp p
Deleting the Linked List // deallocate memory while (head) { entry* n = head->next; delete head; head = n; } head 17 0 0 42 97 35 n
After Deleteing 1st Node // deallocate memory while (head) { entry* n = head->next; delete head; head = n; } head 17 0 0 97 35 n
Variations of a Linked List // create 4 more nodes for (inti = 0; i < 4; i++) {cin >>temp‑>value; temp‑>next = new entry; temp = temp‑>next; } int n; cout << “How many nodes ? “; cin >> n; // create n-1 more nodes for (inti = 0; i < n-1; i++) {cin >>temp‑>value; temp‑>next = new entry; temp = temp‑>next; }
Summary Memory from the heap Dynamic memory allocation using the new operator Build a dynamic linked list Another way of traversing a linked list Deallocating memory using delete operator Dynamic linked list variation