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
Program Stack Operating System Heap
int * ptr; ptr = new int; Program Stack Operating System Heap 9ab int ptr 9ab
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)
// 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)
struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list temp 0 head
/*allocate the first node*/ head= new entry; temp = head; 76c temp 76c head (76c)
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 }
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)
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)
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)
head temp
head temp temp ‑ >value = 0; /*last node points to nothing*/ temp ‑ >next = 0; /*it's a dummy node*/
head temp // traverse the list for (entry* p = head; p; p = p->next) { cout value next << endl; } p
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
head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }
head n // deallocate memory while (head) { entry* n = head->next; delete head; head = n; }
// 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; }
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