Two Way Linked List
Two Way Linked List
59
Other variation of Linked List
60
Two-Way List
61
Two-Way List
• A two-way list is a linear collection of data
element called nodes where each node N
is divided into three parts:
– A information field INFO which contains the
data of N
– A pointer field FORW which contains the
location of the next node in the list
– A pointer field BACK which contains the
location of the preceding node in the list
62
Two-Way List
• List requires two pointer variables:
– FIRST: which points to the first node in the list
– LAST: which points to the last node in the list
INFO field
BACK pointer
FORW pointer
63
Two-Way List
FIRST LAST
64
Two-Way List
FIRST LAST
A B
Forward Pointer
to Node A Backward Pointer to
Node A
65
Two-Way List
Suppose LOCA and LOCB are the locations
of nodes A and B respectively in a two-way
list.
FIRST LAST
A B
LOCA LOCB
• Traversing
• Searching
• Deleting
• Inserting
68
Insertion in Two-Way List
INSERT NODE NEW
LOCA->FORW->BACK = NEW
FIRST NEW->FORW = LOCA->FORW
LAST
A B C
LOCA
LOCA->FORW = NEW
NEW NEW->BACK = LOCA
69
Insertion in Two-Way List
INSERT NODE NEW
LOCA->FORW->BACK = NEW
FIRST NEW->FORW = LOCA->FORW
LAST
A B C
LOCA
LOCA->FORW = NEW
NEW NEW->BACK = LOCA
70
Deletion in Two-Way List
DELETE NODE B
FIRST LAST
A B C
LOCB
LOCB->BACK->FORW = LOCB->FORW
71
Deletion in Two-Way List
DELETE NODE B
FIRST LAST
A B C
LOCB
LOCB->FORW->BACK = LOCB->BACK
72