Professional Documents
Culture Documents
have to find the node before the tail (the last node) by link hopping.
head tail
This problem can be easily solved by using the double linked list.
A node in a doubly linked list: A compound object that
stores a reference to an element and two references, called
next and prev, to the next and previous nodes, respectively.
Element
Node
Reference to an
element
Reference to next
next node
prev Reference to
previous node
For convenience, a doubly linked list has a header node and a
trailer node. They are also called sentinel nodes, indicating
both the ends of a list.
header trailer
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
50D0
5080
5060 50E0
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 50F0
5070 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
50D0 trailer prev
5080
next
5080 50E0 Rome Element
0
5070 0
5050 Seattle
50D0
5080
5060 50E0
50A0 Baltimore
5060 50C0
5050 50C0
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;
}
}
Before the insertion:
header trailer
header trailer
Toronto
header trailer
Toronto
header trailer
header trailer
header trailer
Seattle
((trailer.getPrev()).getPrev()).setNext(trailer)
;
trailer.setPrev((trailer.getPrev()).getPrev());
trailer.prev.prev.next trailer;
trailer.prev trailer.prev.prev;
After the deletion:
header trailer
//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;
}
}
Recall that a queue has a front and a rear. Elements can be
added at the rear and can be removed at the front.
Front Rear
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 ()
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();
}