Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Doubly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly 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 A B C D p A B C p What are the instructions? D A B C 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
Doubly-Linked List in Java © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly-Linked List in Java, 2 Lines 26-27: Why are the parameters different? Why do we need line 28? © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly-Linked List in Java, 3 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly-Linked List in Java, 4 Do we want to put lines 69 and 70 before line 68? Why? © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
Doubly-Linked List in Java, 4 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists
== vs equals() Person person1, person2 What is the difference between person1.equals(person2) © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists