two way linked list

16
Tow Way Linked array(list) The one way linked list gives us the facility of accessing the list in just one direction. The two way linked list gives us the facility of accessing the list in two direction that is in forward and in backward direction. In this each node consist of three nodes: 1. The Right pointer(RP): which keeps the address of successor node. 2. Left pointer (LP): which keeps the address of predecessor node. 3. Information Field (INFO): which keeps the data of the node. Double Linked array has two pointers: HEAD : keeps the address of the first node. TAIL: Keeps the address of last node.

Upload: sami119

Post on 14-May-2017

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Two Way Linked List

Tow Way Linked array(list)• The one way linked list gives us the facility of accessing the

list in just one direction. The two way linked list gives us the facility of accessing the list in two direction that is in forward and in backward direction.

• In this each node consist of three nodes: 1. The Right pointer(RP): which keeps the address of

successor node. 2. Left pointer (LP): which keeps the address of predecessor

node. 3. Information Field (INFO): which keeps the data of the

node.• Double Linked array has two pointers:

HEAD : keeps the address of the first node.TAIL: Keeps the address of last node.

Page 2: Two Way Linked List

Two way link list continue….

• Starting from HEAD and following RP ,we can traverse the list in forward direction.

• Similarly starting from TAIL and following LP,we can traverse the list in backward direction.

• The first node’s LP and last node’s RP are nil,because there is no node on the left side of 1st node and on the right of the last node.

Page 3: Two Way Linked List

Operations on Double link list Creating algorithm:-• Create a node. P:=GETNODE• Make it HEAD of the list ie. HEAD :=P• Make the left pointer nil LP(P):=NIL• Copy data and make right pointer nil INFO[P]:=Data RP(P):=Nil5. Set another pointer Q := P6. If more node nor required , go to step 10.7. Get a node P := GETNODE8. Link with predecessor Rp(q):= p and LP(P):= q9. Go to step 4.10.Make the TAIL TAIL:= p11. EXIT.

Page 4: Two Way Linked List

Traversing Algorithm

Algorithm: Forward• Set a pointer to the HEAD P:= Head• Set a loop while (P Nil) Loop start 1. Process the node , INFO(P) 2. Go forward , P:= RP(P) End of Loop• Algorithm: Forward traversing3. Exit

Page 5: Two Way Linked List

Backward TraversingAlgorithm: Backward traversing1. Set a pointer to the TAIL , P:= TAIL2. Set a LOOP, While (p Nil) Loop start 1. Process the node, INFO(P) 2. Go backward P:= LP(P) End of Loop3. EXIT

Page 6: Two Way Linked List

Searching Algorithm

• To search a particular node in double linked list we need an information KEY with which we will compare the INFO Field of all nodes.

-- Forward searching-- Backward searching

Page 7: Two Way Linked List

Forward searching• Algorithm: Forward searching1. Set a pointer to the HEAD P:= HEAD2. Set a loop, while (INFO(P) Key) and (P nil)3. Go forward P:= RP(P) End of loop4. If p = Nil then Write : “Search unsuccessful” ELSE Write : “ search Successful”5. EXIT.

Page 8: Two Way Linked List

Backward searching

• ASSIGGNMENT to be solved the class

Page 9: Two Way Linked List

Insertion

• Insertion before a given node• Insertion after a given node.

Page 10: Two Way Linked List

Insertion before a given node• Algorithm: suppose our desired node is P, before we want to

insert a new node.1. Get a Node R:= GETNODE2. Copy data INFO[R] := DATA LP(R ):= nil RP( R ):= nil3. Set q to to the left node of P, q:= LP(P)4. Check for the HEAD If q= Nil then, HEAD:= R ELSE, RP(q) := R5. Connect with the predecessor LP(R ):= q6. Connect with the successor RP(R ):= P and LP( P) :=R7. EXIT

Page 11: Two Way Linked List

Insertion after a given node• Algorithm: Suppose our desired node is P, after which we want to

insert new node. 1. Get a node R:= GETNODE2. COPY data INFO[R]:= Data RP(R ):= nil LP(R ):= nil3.Set Q to the right pointer of P, Q:= RP(P)4. Check for the TAIL, if Q= Nil Then,TAIL:= R LP(Q):= R5. Connect with the predecessor, RP(P):= R and LP(R ):=P6.Connect with Successor, RP(R ):= Q and LP(Q):= R7. EXIT

Page 12: Two Way Linked List

Deletion in two way link list• Algorithm: If Q is the required node to be deleted.1. Set pointer to the left and write pointer of Q. P:= LP(Q) R:= RP(Q)2. If ( P= nil and R=nil) then i. HEAD:= NIL ii. TAIL=Nil Else If (P=Nil and R!=Nil) then i. |HEAD:= R ii. LP(R ):= P Else

Page 13: Two Way Linked List

Continue………………If( P!= Nil and R= Nil) then i. TAIL:= P ii. RP(P):= R Else i. RP(P ):= R ii. LP(R ) := P3.Remove the Node Q Delete Q4. Exit

Page 14: Two Way Linked List

Circular link list• A Linked list in which the last node is connected

back with the 1st node, is called circular link list.• In ordinary list the facility to access the 1st node

directly from the last node is not available, But circular link list provides us the facility of coming back on the 1st node directly from the last node without re-accessing the HEAD or any other pointer of the list.

Page 15: Two Way Linked List

Traversing of Circular linked list

1. P:= HEAD2. Repeat I. INFO(P) ii. P:= LINK(P) UTILP= HEAD3. Exit

Page 16: Two Way Linked List

Searching• Algorithm: This algorithm search for information KEY.1. P:= HEAD2. FOUND:= FALSE3. Repeat If INFO(P) = Key Then Found:= True Else P:= LINK(P) Until (P= HEAD) Or (FOUND)4. If Found , write: “search successful”

Else Write:”Search unsuccessful”

5. Exit