12.07.2015 Views

Data Structures and Algorithms

Data Structures and Algorithms

Data Structures and Algorithms

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2. LINKED LISTS 15The following algorithms for the doubly linked list are exactly the same asthose listed previously for the singly linked list:1. Searching (defined in §2.1.2)2. Traversal (defined in §2.1.4)2.2.1 InsertionThe only major difference between the algorithm in §2.1.1 is that we need toremember to bind the previous pointer of n to the previous tail node if n wasnot the first node to be inserted into the list.1) algorithm Add(value)2) Pre: value is the value to add to the list3) Post: value has been placed at the tail of the list4) n ← node(value)5) if head = ∅6) head ← n7) tail ← n8) else9) n.Previous ← tail10) tail.Next ← n11) tail ← n12) end if13) end AddFigure 2.5 shows the doubly linked list after adding the sequence of integersdefined in §2.1.1.Figure 2.5: Doubly linked list populated with integers2.2.2 DeletionAs you may of guessed the cases that we use for deletion in a doubly linkedlist are exactly the same as those defined in §2.1.3. Like insertion we have theadded task of binding an additional reference (P revious) to the correct value.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!