Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists.

Similar presentations


Presentation on theme: "Linked Lists."— Presentation transcript:

1 Linked Lists

2 Linked List Linked List : List implemented with Nodes connected by pointers

3 Node Node: Stores one item of data Stores a pointer to another node

4 Node Use Linked List Nodes: Nodes created on heap, tracked w/pointer

5 Nodes, New & Pointers new requires a pointer to store address

6 Nodes, New & Pointers new requires a pointer to store address
Not every pointer requires new memory

7 Node Connections Connect by setting next ptr…

8 Node Connections Connect by setting next ptr…

9 Node Connections Can reach anything from first Node:

10 Building a List Building list at head:

11 Linked List Temp tracks old head…

12 Linked List Temp becomes new head

13 Linked List Add another node:

14 Linked List Iteration Need pointer to track our position in list:

15 Linked List Iteration Start traversal at head:

16 Linked List Iteration Advance with current = current->next

17 Linked List Iteration Advance with current = current->next

18 Linked List Iteration Advance with current = current->next

19 Linked List Iteration Stop when current = nullptr

20 Linked List Iteration Do work using current->element

21 Tails How do we add something to the end of the list?

22 Tails How do we add something to the end of the list?

23 Linked List Tail Tail pointer tracks last node:

24 Linked List Tail Add to head unchanged…

25 Linked List Tail Add to head unchanged…

26 Linked List Tail Can insert after tail:

27 Linked List Tail Can insert after tail:

28 Linked List Tail New node becomes the new tail


Download ppt "Linked Lists."

Similar presentations


Ads by Google