Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traversing a Linked List

Similar presentations


Presentation on theme: "Traversing a Linked List"— Presentation transcript:

1 Traversing a Linked List
CSCE 121

2 Traversing a Linked List
Use a pointer Head 11 3 8 … unless you keep track, e.g. with a member variable. Current

3 Traversing a Linked List
You do not know how many nodes you have. Head 11 3 8 … unless you keep track, e.g. with a member variable. Current

4 Traversing a Linked List
Don’t use Head to traverse the list! Head 11 3 8

5 Traversing a Linked List
If you do, then you lose access to the first item! Head 11 3 8 Memory Leak!!!

6 Traversing a Linked List
Head 11 3 8 Current

7 Traversing a Linked List
Head 11 3 8 Current

8 Traversing a Linked List
Head 11 3 8 Current

9 Traversing a Linked List
We know we’ve reached the end of the list when current->next is equal to the nullptr. Head 11 3 8 Current

10 Traversing Linked List Pseudocode
current ← head While not at end of list Process node (e.g. output, set, etc.) current ← next(current) Recall, at the end of the list, next points to nullptr. Node* current = head; while current { do_stuff(current); current = current->next; }


Download ppt "Traversing a Linked List"

Similar presentations


Ads by Google