Doubly Linked Lists or Two-way Linked Lists 12/8/2018 Doubly Linked Lists or Two-way Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly Linked List (or Two-way Linked List) A doubly linked list can be traversed forward and backward Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next element node trailer header nodes/positions elements © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Header and trailer Make implementation easier Every insertion/deletion is between two nodes Can be implemented without them Save space Need to consider special cases at the beginning and at the end © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Insertion p A B C p q A B C What are the instructions? X p q A B X C Insert a new node, q, between p and its successor. p A B C p q A B C What are the instructions? X p q A B X C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Deletion (don’t forget to deallocate the deleted node) Remove a node, p, from a doubly linked list. A B C D p A B C p What are the instructions? D A B C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
== (equality) struct Person person1, person2; What is the difference between person1 == person2 Person3 == person4 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists