1 / 20

Dynamic Linked List Lesson xx

Dynamic Linked List Lesson xx. 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.

kevlyn
Télécharger la présentation

Dynamic Linked List Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Linked List Lesson xx

  2. 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

  3. Memory Setup Operating System Program Stack Heap

  4. Operator new Operating System int * ptr; ptr = new int; Program Stack ptr 9ab Heap 9ab int

  5. 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*/

  6. (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; }

  7. Starting the Linked List struct entry { int value;   entry* next; }; int main() {   entry* temp, * head = 0; // initially empty list temp head 0

  8. Create 1st Node /*allocate the first node*/ head= new entry; temp = head; (76c) temp 76c head 76c

  9. 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

  10. 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

  11. 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

  12. 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

  13. End of the Loop head 42 97 35 17 temp

  14. 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

  15. 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

  16. 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

  17. Deleting the Linked List // deallocate memory   while (head)   {     entry* n = head->next;     delete head;     head = n;   } head 17 0 0 42 97 35 n

  18. After Deleteing 1st Node // deallocate memory   while (head)   {     entry* n = head->next;     delete head;     head = n;   } head 17 0 0 97 35 n

  19. 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;   }

  20. 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

More Related