Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

Algorithm for Inserting a node after or before particular node in Circular Doubly Linked List

  Shared By: Lu Fischer    Date: Jul 14    Category: C++ Programming    Views: 7832

Answer:

PROCEDURE INSERT_CD(T, KEY, POS)
[Where pointer ‘T’ is a pointer which can be either pointing to first in or lasting element and key is the value after or before u want to insert new node and pos is a variable which is giving direction ‘right’ or ‘left’ of key value.]

1. [Checking the value of pointer and traversing the destination]

if (T==HEAD)
while (DATA(T) != KEY)
T <-- RIGHT (T)
else
while (DATA(T) != KEY)
T <-- LEFT (T).

2. [Inserting an element to the right of the destination]

if (POS == ‘R’)
if (T == P)
Call GETNODE (S)
DATA (S) <-- ‘xyz’
RIGHT (P) <-- S
LEFT (S) <-- P
RIGHT (S) <-- HEAD
LEFT (HEAD) <-- S
P <-- S
else
Call GETNODE (S)
DATA (S) <-- ‘xyz’
RIGHT (S) <-- RIGHT (T)
LEFT (S) <-- T
RIGHT (T) <-- S
LEFT (RIGHT(S)) <-- S
P <-- S.

3. [Inserting an element to the left side of destination]

else if (T == HEAD)
Call GETNODE (S)
DATA (S) <-- ‘xyz’
RIGHT (S) <-- HEAD
LEFT (HEAD) <-- S
LEFT (S) <-- P
RIGHT (P) <-- S
HEAD <-- S


else
Call GETNODE (S)
DATA (S) <-- ‘xyz’
RIGHT (S) <-- T
LEFT (S) <-- LEFT (T)
LEFT (T) <-- S
RIGHT (LEFT(S)) <-- S.

4. [FINISH]
return.

Share: 
 


Related Topics:

Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: