1 / 14

Linked List Traversal Lesson xx

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

rafer
Télécharger la présentation

Linked List Traversal 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. Linked List TraversalLesson xx

  2. Objectives • Review building a complete linked list • List traversal in main ( ) • List traversal using a function

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

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

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

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

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

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

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

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

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

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

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

  14. Summary Building a complete linked list List traversal in main ( ) List traversal using a function

More Related