Linked Lists
Array List Issues Painful insert/remove at start/middle
Array List Issues Painful insert/remove at start/middle Average ~n/2 wasted space – Worse if shrinks
Array List Issues Painful insert/remove at start/middle Average ~n/2 wasted space – Worse if shrinks Data kept in a single block – Memory fragmentation may prevent grow(): array other data
Linked List Linked List : Implement List ADT with linked series of nodes – One value per node – Pointer to next node
Linked List Implementation will change performance – Space always based on current size of list But worst case larger than array – Data distributed in lots of little pieces + no worries about allocating big blocks - cache coherence – BigO's will change
Linked List List Node: – Stores one item of data – Stores a pointer to another node
Linked List Linked ListNodes: – Each node starts with nullptr for next value
Linked List Linked ListNodes: – Each node starts with nullptr for next value – Set next to address of other node to form a chain – End of chain still points to null
Linked List Goal : List ADT implemented with LinkNodes What does list need to keep track of? Bare minimum: Where first node is
Traversal Traverse Linked Lists with pointer – Start at head
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next – Stop when you hit null
Putting It Together Print each item:
listSize Size of list not explicitly stored Find by traversing and counting: int listSize() count = 0 current = head while current is not null count++ current = current->next return count
Partial Traversal Which node is at index 2? How do we retrieve the value?
Partial Traversal Which node is at index 2? How do we retrieve the value? Start from head (0) Advance to next twice Return data
Partial Traversal retrieveAt(num) Start from head (0) Advance to next num times Print what is there
Partial Traversal retrieveAt(num) Start from head (0) Advance to next num times If hit nullptr, error Print what is there current next on nullptr will blow up
Remove Start How do we remove the first node?
Remove Start Bad attempt 1: head head->next – node1 is now garbage need to delete but no pointer!
Remove Start Bad attempt 2: delete head – Lost track of node 2
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
Insert Start Now we want to add 12 to start of list…
Insert Start Now we want to add 12 to start of list – Need a new Node InsertFirst(12) Node* temp = new Node(12)
Insert Start Now we want to add 12 to start of list – new node will point to old head InsertFirst(12) Node* temp = new Node(12) temp->next = head
Insert Start Now we want to add 12 to start of list – head can then point to temp InsertFirst(12) Node* temp = new Node(12) temp->next = head head = temp
Insert At Want to insert a new value at index 2
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value)
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 2 times
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 2 times TOO FAR No way to adjust node2's pointer!
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next temp->next = newNode
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times newNode->next = temp->next temp->next = newNode What special case still won't work?
Insert At Want to insert a new value at index InsertAt(index, value) If index = 0 InsertFirst(value) Else Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times If temp == nullptr error newNode->next = temp->next temp->next = newNode