Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Hensley Modified over 9 years ago
3
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
4
Program Stack Operating System Heap
5
int * ptr; ptr = new int; Program Stack Operating System Heap 9ab int ptr 9ab
6
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 (int i = 0; i >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 1)
7
// traverse the list for (entry* p = head; p; p = p->next) { cout value next << endl; } // deallocate memory while (head) { entry* n = head->next; delete head; head = n; } return 0; } (Part 2)
8
struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list temp 0 head
9
/*allocate the first node*/ head= new entry; temp = head; 76c temp 76c head (76c)
10
76c temp 76c head 42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node }
11
76c temp 76c head 88b 42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b)
12
88b temp 76c head 88b42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b)
13
4af temp 76c head 88b42 (76c) for (int i = 0; i >temp ‑ >value; /*put address of next cell in.next member */ temp ‑ >next = new entry; temp = temp ‑ >next; //temp now points to last node } (88b) 97 4af (4af)
14
429735 17 head temp
15
429735 170 head temp temp ‑ >value = 0; /*last node points to nothing*/ temp ‑ >next = 0; /*it's a dummy node*/
16
429735 170 head temp // traverse the list for (entry* p = head; p; p = p->next) { cout value next << endl; } p
17
a6e temp 76c head (76c) 88b42 97 (88b) 4af 57d35 (4af) a6e1700 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) 0 p
18
429735 170 head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }
19
9735 170 head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }
20
// create 4 more nodes for (int i = 0; i >temp ‑ >value; temp ‑ >next = new entry; temp = temp ‑ >next; } int n; cout << “How many nodes ? “; cin >> n; // create n-1 more nodes for (int i = 0; i >temp ‑ >value; temp ‑ >next = new entry; temp = temp ‑ >next; }
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.