Sunday, 23 July 2017

Singly Linked List - Insertion Algorithm - Data Structures



In this section, we will see how a new node is added into an already existing linked list. There are five situation for inserting new node. They are as follows...

  1. Inserting At Beginning of the list
  2. Inserting At End of the list
  3. Inserting a node at a specific position
  4. Inserting after a given node
  5. Inserting before a given node

Inserting a new node in beginning of linked list

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 = NEW_NODE

Step 4 : EXIT

Inserting a node at the end of a linked list

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

Step 4 : EXIT

Counting total no of nodes in a singly linked list

int countnode(node *ptr)
{

int num_of_node=0;
while(ptr != NULL)
{

num_of_node++;
ptr = ptr -> next;

}

return (num_of_node);

}

Inserting a node at a specific position / mid in a linked list

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

totalnode = countnode(START)

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

IF( POS > 1 && POS < totalnode)

Step 3: Create the NEW_NODE using createnode()

NEW_NODE = createnode();

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++;
}
NEWNODE -> NEXT = PTR -> NEXT;
PTR -> NEXT = NEWNODE;
[END OF IF]

ELSE
Write "Position is invalid "

Step 5 : EXIT

Algorithm for traversing a singly linked list

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

Write "Sorry Buddy list is empthy "

Step 2: PTR = START

while ( PTR != NULL )
{

printf("%d-->",PTR -> DATA);
PTR = PTR -> NEXT

}
printf("X");

Step 3: EXIT

NOTE : List must contain more than two node and node after/before which u want to insert must present in the middle of list

Inserting a node after a given node

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->DATA !=VALUE)
{
PTR = PTR->NEXT
}
NEW_NODE -> NEXT = PTR->NEXT
PTR->NEXT = NEW_NODE

Step 4 : EXIT

Inserting a node before a given node

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->DATA !=NUM)
{
PTR = PTR->NEXT }
NEW_NODE -> NEXT = PTR->NEXT
PTR->NEXT = NEW_NODE

Step 4 : EXIT


Share this

0 Comment to " Singly Linked List - Insertion Algorithm - Data Structures "

Post a Comment