140 likes | 260 Views
Linked List Traversal Lesson xx. Objectives. Review building a complete linked list List traversal in main ( ) List traversal using a function. Complete List Traversal Program. struct entry { int value; entry* next; };
E N D
Objectives • Review building a complete linked list • List traversal in main ( ) • List traversal using a function
Complete List Traversal Program struct entry { int value; entry* next; }; int main() { entry n1, n2, n3; entry* head = &n1; n1.next = &n2; n2.next = &n3; n3.next = 0; n1.value = 100; n2.value = 200; n3.value = 300; entry * temp; temp= head; // traverse the list while (temp != NULL) {cout << temp << " " << temp->value << " " << temp->next << endl; temp = temp->next; } return 0; }
Code to Build a Linked List entry n1, n2, n3; entry* head = &n1; n1.next = &n2; n2.next = &n3; n3.next = 0; n1.value = 100; n2.value = 200; n3.value = 300; n2 n3 (7fa) (88b) n1 (56c) 88b 200 300 100 7fa n2.value n3.value n2.next n3.next n1.value n1.next head 56c 0
Pointer for Traversing List entry * temp; temp= head; n2 n3 (7fa) (88b) n1 (56c) temp 56c 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Loop for Traversing List while (temp != NULL) {cout << temp << " " << temp->value << " " << temp->next << endl; temp = temp->next; } n2 n3 (7fa) (88b) n1 (56c) temp 56c 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Output After 1stcout temp->next temp temp->value 56c 100 7fa n2 n3 (7fa) (88b) n1 (56c) temp 56c 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Moving to the Next Node temp = temp->next; n2 n3 (7fa) (88b) n1 (56c) temp 7fa 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
2nd Pass Through Loop while (temp != NULL) {cout << temp << " " << temp->value << " " << temp->next << endl; temp = temp->next; } n2 n3 (7fa) (88b) n1 (56c) temp 7fa 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Complete Output 56c 100 7fa 7fa 200 88b 88b 300 0 n2 n3 (7fa) (88b) n1 (56c) temp 0 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Traversing a Linked List Using a Function struct entry { int value; entry* next; }; void printList(const entry*); // prototype int main() { entry n1, n2, n3; n1.next = &n2; n2.next = &n3; n3.next = 0; entry* head = &n1; printList(head); return 0; } void printList(const entry* pointer) { while (pointer != 0 ) { cout << pointer << " " << pointer->value << " " << pointer->next << endl; pointer = pointer->next; } }
Traversing a Linked List Using a Function 1 struct entry { int value; entry* next; }; void printList(const entry*); // prototype int main() { entry n1, n2, n3; n1.next = &n2; n2.next = &n3; n3.next = 0; entry* head = &n1; printList(head); return 0; } void printList(const entry* pointer) { while (pointer != 0 ) { cout << pointer << " " << pointer->value << " " << pointer->next << endl; pointer = pointer->next; } } 2 3
printList ( ) Function void printList(const entry* pointer) { while (pointer != 0 ) { cout << pointer << " " << pointer->value << " " << pointer->next << endl; pointer = pointer->next; } } n2 n3 (7fa) (88b) n1 (56c) pointer 56c 88b 200 300 100 7fa n3.value n2.value n2.next n3.next n1.value n1.next head 56c 0
Summary Building a complete linked list List traversal in main ( ) List traversal using a function