Linked Lists Example • We would like to keep a list of inventory records – but only as many as we need • An array is a fixed size • • Instead – use a linked list What are the disadvantages of using a linked list? Linked List Object Object Object next next next • Node – one element of the linked list – Object – data stored in the node – examples? – next – a reference to the next node in the list • last node points to NULL Ø Linked List tail head Object Object Object next next next • head keeps track of the head of the list • tail keeps track of the last node in the list – tail not always used Ø Insertion at Head tail new_node head Object3 next Insert here Object1 next Object2 next Ø Insertion at Head tail new_node head Object1 next Object2 next Ø Object3 next • Create new_node – store object in new_node • Point new_node next to the node head points to Insertion at Head tail new_node head Object1 next Object2 next Ø Object3 next • Create new_node – store object in new_node • Point new_node next to the node head points to • Point head to new_node Insertion at Head tail head Object3 Object1 next next Object2 next • Does this algorithm work for the list below? new_node tail head Object3 next Ø Ø Insertion at Head • Create new_node – store object in new_node • Point new_node next to the node head points to • Point head to new_node • If tail points to NULL – point tail to new_node Insertion at Head new_node tail head Ø Object3 next • Create new_node – store object in new_node • Point new_node next to the node head points to • Point head to new_node • If tail points to NULL – point tail to new_node Insertion at Tail tail Object1 head next next Object3 new_node next new_node tail head Object3 next Ø Object2 Ø Insert here Find tail head 5 12 3 next next next • find(3) • find(16) - always remember to deal with special cases Ø Deletion tail head Object3 next • Deletion of head – Complexity? • Deletion of tail – Complexity? Object1 next Object2 next Ø Insertion/Deletion in Middle tail head Object3 next Object1 next Object2 next • Insert between Object1 and Object2 • Delete Object1 Ø Exercises • Implement a method to recursively count number of nodes in a linked list • Implement a method to reverse a linked list • Implement a method that takes as input two integers representing node positions and swaps the specified nodes Doubly Linked Lists Object1 header Object2 Object3 prev prev prev next next next trailer • Each node keeps a pointer to the next node and to the previous node – Makes some operations (such as insertion at end) more efficient – Costs? • At the beginning and end of the list are sentinel nodes – Simplify insertion/deletion algorithm Doubly Linked Lists Object1 header Object2 Object3 prev prev prev next next next • Insertion and deletion at beginning/end • Insertion and deletion in middle header trailer trailer new_node Object4 prev Doubly Linked Lists insert here next Object1 header • Insertion Object2 Object3 prev prev prev next next next trailer new_node Object4 prev Doubly Linked Lists insert here next Object1 header • Object2 Object3 prev prev prev next next next trailer Insertion at head 1. Set next of new_node to point to what header’s next points to 2. Set prev of node that header’s next points to to point to new_node 3. Set prev of new_node to point to header 4. Set header’s next to point to new_node • • • Number 1 must come before number 4 Insertion at trailer? Deletion?