Doubly Linked List: - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3
Doubly Linked List: - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3
This problem can be easily solved by using the double linked list.
A node in a doubly linked list: A com pound object that
stores a reference to an elem ent and tw o references, called
next and prev, to the next and previous nodes, respectively.
E lem ent
N ode
Reference to an
elem ent
Reference to next
next node
prev Referenc e to
previous node
F o r c o n v e n ie n c e , a d o u b ly lin k e d lis t h a s a h e a d e r n o d e a n d a
tr a ile r n o d e . T h e y a re a ls o c a lle d s e n tin e l n o d e s , in d ic a tin g
b o th th e e n d s o f a lis t.
50B0
0 5110
5050
50A0 0
5100
5090
5060 50F0
5070
5080 50D0 Node
5080 50E0 Rome prev
0 next
5070 0
Element
5050 Seattle
50D0
5080
5060 50E0
50A0 Baltimore
5060 50C0
50C0
5050
header
50B0
0 5110
5050
50A0 0
5100
5090
5060
5070 50F0
Node
5080 50D0
prev
5080 50E0 Rome next
0
0 Element
5070
5050 Seattle
50D0
5080
5060 50E0
50A0 Baltimore
5060 50C0
5050 50C0
header
50B0
0 5110
5050
50A0 0
5100
5090
5060 Node
5070 50F0
5080 50D0 prev
next
5080 50E0 Rome
0 Element
5070 0
5050 Seattle
5080 50D0
5060 50E0
5100
5090
5060
5070 50F0 Node
5080 50D0 prev
5080 Rome next
50E0
0 Element
5070 0
5050 Seattle
50D0
5080
50E0
5060
50A0 Baltimore
5060 50C0
5050 50C0
header
50B0
0 5110
5050
50A0 0
5100
5090
5060
5070 50F0 Node
5080 50D0
prev
5080 Rome next
50E0
0 Element
5070 0
5050 Seattle
50D0
5080
50E0
5060
50A0 Baltimore
5060 50C0
5050 50C0
header
50B0
0 5110
5050
50A0 0
5100
5090
5060 Node
5070 50F0
5080 50D0 trailer prev
next
5080 50E0 Rome Element
0
5070 0
5050 Seattle
5080 50D0
5060 50E0
50A0 Baltimore
5060 50C0
5050 50C0
Class DLNode
Here is an implementation of nodes for doubly
linked lists in Java:
public DLNode() {
this( null, null, null );
}
Object getElement() {
return element;
}
DLNode getNext() {
return next;
}
DLNode getPrev() {
return prev;
}
}
Insertion of an Element at the
Head
B e fo re th e in se rtio n :
h ea d er tra iler
header trailer
Toronto
header trailer
Toronto
header tr a ile r
header trailer
Seattle
((trailer.getPrev()).getPrev).setNext(trailer);
trailer.setPrev((trailer.getPrev()).getPrev());
trailer.prev.prev.next trailer;
trailer.prev trailer.prev.prev;
A fte r th e d e le tio n :
header tr a ile r
T o ro n to B a ltim o re Rom e
Deleting an element at the head is similar to deleting an element at
the tail.
//System.out.println("swapElement is executed!!!");
DNode pA = checkPosition(a);
DNode pB = checkPosition(b);
Object temp = pA.element();
pA.setElement(pB.element());
pB.setElement(temp);
}
public Position next(Position p)
throws InvalidPositionException, BoundaryViolationException {
DNode v = checkPosition(p);
DNode next = v.getNext();
if (next == trailer)
throw new BoundaryViolationException
("Cannot advance past the beginning of the list");
return next;
}
}
Data Structure Exercises 6.1
Double-Ended Queues
R e c a ll th a t a q u e u e h a s a fr o n t a n d a r e a r . E le m e n ts c a n b e
a d d e d a t th e re a r a n d c a n b e re m o v e d a t th e fro n t.
F ro n t R ear
N o w w e a ls o w a n t to a d d e le m e n ts a t th e fro n t a n d re m o v e
th e m a t th e re a r.
Definition: A double-ended queue is a queue that supports
insertion and deletion at both the front and the rear of the
queue.
Operation Output D
insertFirst(3) - (3)
insertFirst(5) - (5,3)
removeFirst() 5 (3)
insertLast(7) - (3,7)
removeFirst() 3 (7)
removeLast() 7 ()
removeFirst() “error” ()
isEmpty() true ()
Implementing a Deque with a
Doubly Linked List
Recall that elements can be inserted and deleted easily at both
the head and tail of a doubly linked list. Therefore, a deque can
be implemented with a doubly linked list.
Example:
header trailer
header trailer
public Object first() throws DequeEmptyException {
if( isEmpty() )
throw new DequeEmptyException( "Deque is empty." );
return header.getNext().getElement();
}
link hopping
header
Baltimore
public void insertFirst( Object o ) {
DLNode second = header.getNext();
DLNode first = new DLNode( o, header, second );
second.setPrev( first );
header.setNext( first );
size++;
}
second
header first second
header
……
first …
Object o Object o
public Object removeLast() {
if( isEmpty() )
throw new DequeEmptyException( "Deque is empty." );
DLNode last = trailer.getPrev();
Object o = last.getElement();
DLNode secondtolast = last.getPrev();
trailer.setPrev( secondtolast );
secondtolast.setNext( trailer );
size--;
return o;
}
secondtolast
trailer
secondtolast last
trailer
… last
Object o Object o
public Object last() throws DequeEmptyException {
if( isEmpty() )
throw new DequeEmptyException( "Deque is empty." );
return trailer.getPrev().getElement();
}