Singly
Singly
elements
DNode<T> class
It represents a node in doubly linked list
elements
Insertion
We visualize operation insertAfter(DNode<T> p, T X), which returns Dnode<T> q
p
A B C
p
A B q C
X
p q
A B X C
Insertion Algorithm
Algorithm insertAfter(p,e):
Create a new node v
v.setElement(e)
v.setPrev(p) {link v to its predecessor}
v.setNext(p.getNext()) {link v to its successor}
(p.getNext()).setPrev(v) {link p’s old successor to v}
p.setNext(v) {link p to its new successor, v}
return v {the position for the element e}
Deletion
We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
Deletion Algorithm
Algorithm remove(p):
t = p.element {a temporary variable to hold the return value}
(p.getPrev()).setNext(p.getNext()) {linking out p}
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null) {invalidating the position p}
p.setNext(null)
return t