370 likes | 519 Views
Linked Lists. Just another data structure Dynamic Grows and shrinks as necessary A “perfect fit” in memory Allocate memory when we Add() A little bit more code “Slow” access to data. Nodes. Each node has a piece/set of data. Nodes. Each node has a piece/set of data. head. 75. Nodes.
E N D
Linked Lists • Just another data structure • Dynamic • Grows and shrinks as necessary • A “perfect fit” in memory • Allocate memory when we Add() • A little bit more code • “Slow” access to data
Nodes • Each node has a piece/set of data
Nodes • Each node has a piece/set of data head 75
Nodes • Each node has a piece/set of data • Each node has a reference to the “next” thing in the list head 75 “next” is null – meaning “end of list”
Code class Node { int data; Node next;} head 75
Code class Node { int data; Node next; // Is this recursive? Not really…} head 75
Adding Nodes We can add to the end of the list… head 75
Adding Nodes We can add to the end of the list… head 75 21
Adding Nodes We can add to the end of the list… head 75 21 99
Adding Nodes … we can add to the beginning… head 75
Adding Nodes … we can add to the beginning… head 75 temp 13
Adding Nodes … we can add to the beginning… head 75 temp 13
Adding Nodes … we can add to the beginning… head 75 temp 13
Adding Nodes … we can add to the beginning… head 75 13
Adding Nodes … we can add to the beginning… head 75 13
Adding Nodes … or we can add to the middle head 21 75 99
Adding Nodes … or we can add to the middle We want to add 80 head 21 75 99
Adding Nodes … or we can add to the middle Special case: Is 80 < 21? If so, add to the beginning! head 21 75 99
Adding Nodes … or we can add to the middle Look at this node’s next pointer. If so, add to the beginning! head 21 75 99
Adding Nodes … or we can add to the middle Is 80 < 75? Nope… head 21 75 99
Adding Nodes … or we can add to the middle So update the current node and look at the next pointer head 21 75 99
Adding Nodes … or we can add to the middle Is 80 < 99? Yes! head 21 75 99
Adding Nodes … or we can add to the middle Create a temp node for the 80 head 21 75 99 temp 80
Adding Nodes … or we can add to the middle Have temp.next point to the 99 head 21 75 99 temp 80
Adding Nodes … or we can add to the middle Have the 75 node point to the 80 head 21 75 99 temp 80
The Importance of Order What if instead… Have the 75 node’s next pointer point to the 80 head 21 75 99 temp 80
The Importance of Order What if instead… Create a temp node for the 80 head 21 75 99 temp 80
The Importance of Order What if instead… Have temp’s next pointer point to the 75’s next node? head 21 75 99 temp 80
The Importance of Order What if instead… Have temp’s next node point to the 75’s next node? head 21 75 99 temp 80
The Importance of Order What if instead… We’ve lost the rest of our list! head 21 75 99 temp 80
Why? • We only have access to one variable • Traversing from beginning to end head 21 75 99 temp 80
One Other Note head 21 75 80 99 101
One Other Note 75 101 80 head 21 99
Summary • Dynamic • Grows and shrinks as necessary • A “perfect fit” in memory • Insert • Beginning/end/middle • Order is important • “Slow” access to information because of traversal