Doubly Linked Lists or Twoway Linked Lists 2014

  • Slides: 6
Download presentation
Doubly Linked Lists or Two-way Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked

Doubly Linked Lists or Two-way Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 1

Doubly Linked List (or Two-way Linked List) q q A doubly linked list can

Doubly Linked List (or Two-way Linked List) q q A doubly linked list can be traversed forward and backward Nodes store: n n n q element link to the previous node link to the next node prev next element node Special trailer and header nodes/positions header trailer elements © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 2

Header and trailer q Make implementation easier n q Every insertion/deletion is between two

Header and trailer q Make implementation easier n q Every insertion/deletion is between two nodes Can be implemented without them n n Save space Need to consider special cases at the beginning and at the end © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 3

Insertion q Insert a new node, q, between p and its successor. p A

Insertion q Insert a new node, q, between p and its successor. p A B C p A What are the instructions? © 2014 Goodrich, Tamassia, Goldwasser C X p A q B Doubly Linked Lists X C 4

Deletion q (don’t forget to deallocate the deleted node) Remove a node, p, from

Deletion q (don’t forget to deallocate the deleted node) Remove a node, p, from a doubly linked list. A B C What are the instructions? A © 2014 Goodrich, Tamassia, Goldwasser B Doubly Linked Lists p D C 5

== (equality) struct Person person 1, person 2; q struct Person *person 3, *person

== (equality) struct Person person 1, person 2; q struct Person *person 3, *person 4; q q What is the difference between n n person 1 == person 2 Person 3 == person 4 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 6