Monday, 24 July 2017

Doubly Linked List | Insertion Algorithm | Data Structures & Algorithm


Inserting a NEW Node at the Beginning of a Doubly Linked List

doublyll

Step 1: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

Step 2: If the linked list is empthy (START == NULL)

START = NEW_NODE

Step 3: If the linked list is not empthy

NEW_NODE -> NEXT =START
START -> PREV = NEW_NODE
START = NEW_NODE

Step 4 : EXIT

Inserting a new node at the end of a Doubly Linked List

doubly linked list insert

Step 1: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

Step 2: If the linked list is empthy (START == NULL)

START = NEW_NODE

Step 3: If the linked list is not empthy

PTR = START
while(PTR -> NEXT != NULL)
{
PTR = PTR -> NEXT
}
PTR -> NEXT = NEW_NODE
NEW_NODE ->PREV = PTR

Step 4 : EXIT

Inserting a node at a specific position in a Doubly linked list

doublyllinsert

Step 1: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

Step 2: Enter the position where u want to insert the node & count total no of node

totalnode = countnode(START)

Step 3: Ensure that enter postion is in between first and last node

IF( POS > 1 && POS < totalnode)

Step 4: Store the starting address in PTR & move the PTR pointer upto specified position .

PTR = START;
MOVE = 1;
while(MOVE< POS-1)
{
PTR = PTR->NEXT;
MOVE++;
}
NEW_NODE -> NEXT = PTR -> NEXT;
NEW_NODE -> PREV = PTR
PTR -> NEXT -> PREV = NEW_NODE
PTR -> NEXT = NEW_NODE
[END OF IF]

ELSE
Write "Position is invalid "

Step 5 : EXIT


Share this

0 Comment to "Doubly Linked List | Insertion Algorithm | Data Structures & Algorithm"

Post a Comment