You are on page 1of 14

Doubly Linked List

Doubly Linked List


Node Representation
LPTR

INFO

RPTR

Doubly Linked List looks like


L
5

R
6

Doubly Linked List - Insertion

Procedure DOUBINS (L, R, M, X):


L left-most pointer variable
R right-most pointer variable
M The insertion is to be performed to the left of a specified
node with its address given by the pointer variable M. The
X - The information to be entered in the node

Doubly Linked List Insertion


Is the list EMPTY?
1. [Obtain new node from availability stack]
NEW NODE
2. [Copy information field]
INFO(NEW) X
3. [Insert into an empty list?]
If R = NULL
then LPTR(NEW) RPTR(NEW) NULL
L R NEW
Return

LPTR

L=R=NEW

RPTR

Doubly Linked List - Insertion


4. [Left most insertion?]
If M = L
then LPTR(NEW) NULL
RPTR(NEW) M
LPTR(M) NEW
L NEW
Return

L
5

NEW

R
6

Doubly Linked List - Insertion


4. [Left most insertion?]
If M = L
then LPTR(NEW) NULL
RPTR(NEW) M
LPTR(M) NEW
L NEW
Return

NEW

Doubly Linked List - Insertion


5. [Insert in middle]
LPTR(NEW) LPTR(M)
RPTR(NEW) M
LPTR(M) NEW
RPTR (LPTR(NEW)) NEW
Return

X
NEW

L
5

R
6

7
M

Doubly Linked List - Insertion


5. [Insert in middle]
LPTR(NEW) LPTR(M)
RPTR(NEW) M
LPTR(M) NEW
RPTR (LPTR(NEW)) NEW
Return
X
NEW
L
5

R
6

7
M

Doubly Linked List - Deletion


1. [Underflow?]
If R = NULL
then Write(UNDERFLOW)
Return

X
NEW

L
5

R
6

7
M

Doubly Linked List - Deletion


DOUBDEL (L, R, OLD)
2. [Delete node]
If L = R
then L R NULL

L=R
5

Doubly Linked List - Deletion


2. [Delete node]
If L = R
then L R NULL

else
If OLD = L
(Left-most node being deleted)
then
L RPTR(L)
LPTR(L) NULL
else

L
5

If OLD = R
(Right-most node being deleted)
then
R LPTR(R)
RPTR(R) NULL
else
RPTR (LPTR(OLD)) RPTR (OLD)
LPTR (RPTR(OLD)) LPTR (OLD)

6
L
6

R
9
R

Doubly Linked List - Deletion


2. [Delete node]
If L = R
then L R NULL
else If OLD = L
(Left-most node being deleted)
then
L RPTR(L)
LPTR(L) NULL

else If OLD = R
(Right-most node being deleted)
then R LPTR(R)
RPTR(R) NULL
else

L
5

RPTR (LPTR(OLD)) RPTR (OLD)


LPTR (RPTR(OLD)) LPTR (OLD)

R
6

R
7

L
5

Doubly Linked List - Deletion


2. [Delete node]
If L = R
then L R NULL
else If OLD = L
(Left-most node being deleted)
then
L RPTR(L)
LPTR(L) NULL
else

If OLD = R
(Right-most node being deleted)
then
R LPTR(R)
RPTR(R) NULL

else
L
5

RPTR (LPTR(OLD)) RPTR (OLD)


LPTR (RPTR(OLD)) LPTR (OLD)
OLD
6

R
9

L
5

1
R

Questions?

You might also like