0% found this document useful (0 votes)
86 views

Two Way Linked List

A two-way linked list allows traversal in both directions by including backward pointers. Each node contains a forward pointer to the next node, a backward pointer to the previous node, and a data field. The list uses two pointers - one to the first node and one to the last. Nodes can be inserted by adjusting the relevant forward and backward pointers, and deleted by changing the pointers on either side of the target node.

Uploaded by

King Shah 673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Two Way Linked List

A two-way linked list allows traversal in both directions by including backward pointers. Each node contains a forward pointer to the next node, a backward pointer to the previous node, and a data field. The list uses two pointers - one to the first node and one to the last. Nodes can be inserted by adjusting the relevant forward and backward pointers, and deleted by changing the pointers on either side of the target node.

Uploaded by

King Shah 673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Other variation of Linked List

• A linked list whose last node points back to


the first node instead of containing a NULL
pointer called a circular list

59
Other variation of Linked List

• A linked list which contains both a special


header node at the beginning of the list
and a special trailer node at the end of list

Header Node Trailer Node

60
Two-Way List

• What we have discussed till now is a one-


way list [ Only one way we can traversed
the list]
• Two-way List : Can be traversed in two
direction
– Forward : From beginning of the list to end
– Backward: From end to beginning of the 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.

The statement that node B follows node A is


equivalent to the statement that node A
precedes node B

Pointer Property: LOCA->FORW = LOCB if


and only if LOCB->BACK = LOCA
66
Two-Way List

FIRST LAST

A B
LOCA LOCB

LOCA->FORW = LOCB if and only if


LOCB->BACK = LOCA 67
Operation in two-way list

• 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

You might also like