Você está na página 1de 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.

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 nodes LP and last nodes RP are nil,because there is no node on the left side of 1st node and on the right of the last node.

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):=Nil 5. Set another pointer Q := P 6. If more node nor required , go to step 10. 7. Get a node P := GETNODE 8. Link with predecessor Rp(q):= p and LP(P):= q 9. Go to step 4. 10.Make the TAIL TAIL:= p 11. EXIT.

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 traversing 3. Exit

Backward Traversing
Algorithm: Backward traversing 1. Set a pointer to the TAIL , P:= TAIL 2. Set a LOOP, While (p Nil) Loop start 1. Process the node, INFO(P) 2. Go backward P:= LP(P) End of Loop 3. EXIT

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

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

Backward searching
ASSIGGNMENT to be solved the class

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

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:= GETNODE 2. Copy data INFO[R] := DATA LP(R ):= nil RP( R ):= nil 3. 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) := R 5. Connect with the predecessor LP(R ):= q 6. Connect with the successor RP(R ):= P and LP( P) :=R 7. EXIT

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:= GETNODE 2. COPY data INFO[R]:= Data RP(R ):= nil LP(R ):= nil 3.Set Q to the right pointer of P, Q:= RP(P) 4. Check for the TAIL, if Q= Nil Then,TAIL:= R LP(Q):= R 5. Connect with the predecessor, RP(P):= R and LP(R ):=P 6.Connect with Successor, RP(R ):= Q and LP(Q):= R 7. EXIT

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

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

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.

Traversing of Circular linked list


1. P:= HEAD 2. Repeat I. INFO(P) ii. P:= LINK(P) UTILP= HEAD 3. Exit

Searching
1. 2. 3. Algorithm: This algorithm search for information KEY. P:= HEAD FOUND:= FALSE 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

Você também pode gostar