Example • We would like to keep a list of inventory records – but only as many as we need Linked Lists • An array is a fixed size • Instead – use a linked list • What are the disadvantages of using a linked list? Linked List Linked List tail Object Object Object Object Object Object head next next next next next next Ø Ø • Node – one element of the linked list • head keeps track of the head of the list – Object – data stored in the node – examples? • tail keeps track of the last node in the list – next – a reference to the next node in the list – tail not always used • last node points to NULL Insertion at Head Insertion at Head tail tail new_node new_node Object1 Object2 Object1 Object2 head head next next next next Ø Ø Object3 Object3 Insert here next next • Create new_node – store object in new_node • Point new_node next to the node head points to 1
Insertion at Head Insertion at Head tail tail new_node Object1 Object2 Object3 Object1 Object2 head head next next next next next Ø Ø Object3 next • Create new_node • Does this algorithm work for the list below? – store object in new_node • Point new_node next to the node head points to new_node tail head Ø • Point head to new_node Object3 next Insertion at Head Insertion at Head new_node tail • Create new_node Ø head – store object in new_node Object3 next • Point new_node next to the node head points to • Create new_node • Point head to new_node – store object in new_node • Point new_node next to the node head points to • If tail points to NULL • Point head to new_node – point tail to new_node • If tail points to NULL – point tail to new_node Insertion at Tail Find tail tail Object1 Object2 head next next Ø 5 12 3 head Object3 Insert here new_node next next next next Ø • find(3) new_node tail • find(16) - always remember to deal with special Ø head cases Object3 next 2
Deletion Insertion/Deletion in Middle tail tail Object3 Object1 Object2 Object3 Object1 Object2 head head next next next next next next Ø Ø • Deletion of head • Insert between Object1 and Object2 – Complexity? • Delete Object1 • Deletion of tail – Complexity? Doubly Linked Lists Doubly Linked Lists Object1 Object2 Object3 Object1 Object2 Object3 prev prev prev prev prev prev header header trailer trailer next next next next next next • Each node keeps a pointer to the next node and • Insertion and deletion at beginning/end to the previous node – Makes some operations (such as insertion at end) • Insertion and deletion in middle more efficient – Costs? • At the beginning and end of the list are sentinel header trailer nodes – Simplify insertion/deletion algorithm new_node new_node Doubly Linked Lists Doubly Linked Lists Object4 Object4 prev prev insert here insert here next next Object1 Object2 Object3 Object1 Object2 Object3 prev prev prev prev prev prev header header trailer trailer next next next next next next • Insertion at head • Insertion 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? 3
Recommend
More recommend