Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List Intro CSCE 121 J. Michael Moore.

Similar presentations


Presentation on theme: "Linked List Intro CSCE 121 J. Michael Moore."— Presentation transcript:

1 Linked List Intro CSCE 121 J. Michael Moore

2 Array Recall Linked List
Arrays are created to be a specific size. Once you run out of slots, you can’t add any more elements. Now that you know how to use dynamic memory, you could create a new larger array and copy the elements over. That’s what vector does! Linked List Can grow as large as needed.

3 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

4 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

5 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

6 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

7 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

8 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

9 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

10 Array Insert Insert 11 into the first position (i.e. index 0)
1 2 3 4 5 6 11 9 7 Insert 11 into the first position (i.e. index 0) Shift all elements Insert

11 Note: Data could be complex like a Class, or simple like an int.
Dynamic Alternative Note: Data could be complex like a Class, or simple like an int. Create Node Contains Data Contains Pointer/Reference to next element 11

12 Linked List Program starts with a pointer to the first node in the list. Normally pointer to start node is called head. Set to nullptr if the list is empty.

13 Add Item to Front of List
Create a new node Head ? ?

14 Add Item to Front of List
Create a new node Set its value Head ? 11

15 Add Item to Front of List
Create a new node Set its value Attach to list Set Node’s next to head Head ? 11

16 Add Item to Front of List
Create a new node Set its value Attach to list Set Nodes next to head Set Head to point to new node. Head What is this? ? 11

17 Add Item to Front of List
Create a new node Set its value Attach to list Set Nodes next to head Set Head to point to new node. Head What is this? 11 If it was added to an empty list…

18 Add Item to Front of List
Create a new node Set its value Attach to list Set Nodes next to head Set Head to point to new node. Head What is this? 11 33 If it was added to a non empty list…

19 Linked List vs. Array I’m sure I forgot something, but you get the idea! Linked List Array More memory Faster to insert item in middle. Slower to get to item in list. Can grow as needed. Less memory Slower to insert item in middle. Faster to get to item in list. Fixed size. When it runs out of room the vector class: creates a new larger array on the heap copies the values to the new one deletes the old one Vector uses an array, but is resizable. HOW?


Download ppt "Linked List Intro CSCE 121 J. Michael Moore."

Similar presentations


Ads by Google