Now we will see how to insert a new node at position N in circular doubly link list.
Algorithm for insertion at position N in circular doubly link list:
In this algorithm, START is pointer to the first node of the list, PTR is node to be inserted and POS is position where PTR is to be inserted. The step are as below:- Create new node PTR
- Set the info field of PTR
- If POS < 1
- Invalid Insertion
- If POS == 1
- If list have no node i.e. START == NULL
- Set START = PTR
- Set PTR->NEXT = PTR
- Set PTR->PREV = PTR
- Else
- Set PTR->NEXT = START
- Set PTR->PREV = START->PREV
- Set START->PREV->NEXT = PTR
- Set START->PREV = PTR
- Set START = PTR
- End If;
- If list have no node i.e. START == NULL
- Else
- If START == NULL
- Insertion not possible
- Return
- End If
- TEMP = START
- Traverse the list for (POS-1)th node into TEMP pointer
- If TEMP is last node i.e. TEMP->NEXT == START
- Set PTR->PREV = TEMP
- Set PTR->NEXT = TEMP->NEXT
- Set TEMP->NEXT->PREV = PTR
- Set TEMP->NEXT = PTR
- Else
- Set PTR->NEXT = TEMP->NEXT
- Set PTR->PREV = TEMP
- Set TEMP->NEXT->PREV = PTR
- Set TEMP->NEXT = PTR
- End If;
- If START == NULL
- End If;
Function to insert node at position N in circular doubly link list:
void insertAtPosition(NODE **start, int info, int pos){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
NODE *temp;
ptr->info = info;
if(pos < 1){ printf("\nError : Invalid position"); free(ptr); } else if(pos == 1){ if(*start == NULL){ *start = ptr; ptr->next = ptr;
ptr->prev = ptr;
}
else{
ptr->next = *start;
ptr->prev = (*start)->prev;
(*start)->prev->next = ptr;
(*start)->prev = ptr;
*start = ptr;
}
}
else{
if(*start == NULL){
printf("\nNode insertion not possible as list is empty");
free(ptr);
return;
}
temp = *start;
for(int i = 2; i < pos; i++){ temp = temp->next;
if(temp == *start){
printf("\nError: Invalid position\n");
free(ptr);
return;
}
}
if(temp->next == *start){
ptr->prev = temp;
ptr->next = temp->next;
temp->next->prev = ptr;
temp->next = ptr;
}
else{
ptr->next = temp->next;
ptr->prev = temp;
temp->next->prev = ptr;
temp->next = ptr;
}
}
}
Program to insert at position N in the circular doubly link list:
#include <stdio.h>
#include <malloc.h>
typedef struct node{
struct node *prev;
int info;
struct node *next;
} NODE;
void insertAtPosition(NODE **, int, int);
void traverse(NODE **);
int main(){
NODE *start = NULL;
insertAtPosition(&start, 3, 2);
traverse(&start);
insertAtPosition(&start, 24, 1);
traverse(&start);
insertAtPosition(&start, 4, 2);
traverse(&start);
insertAtPosition(&start, 4, 2);
traverse(&start);
insertAtPosition(&start, 4, 4);
traverse(&start);
insertAtPosition(&start, 4, 6);
traverse(&start);
return 0;
}
void insertAtPosition(NODE **start, int info, int pos){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
NODE *temp;
ptr->info = info;
if(pos < 1){ printf("\nError : Invalid position"); free(ptr); } else if(pos == 1){ if(*start == NULL){ *start = ptr; ptr->next = ptr;
ptr->prev = ptr;
}
else{
ptr->next = *start;
ptr->prev = (*start)->prev;
(*start)->prev->next = ptr;
(*start)->prev = ptr;
*start = ptr;
}
}
else{
if(*start == NULL){
printf("\nNode insertion not possible as list is empty");
free(ptr);
return;
}
temp = *start;
for(int i = 2; i < pos; i++){ temp = temp->next;
if(temp == *start){
printf("\nError: Invalid position\n");
free(ptr);
return;
}
}
if(temp->next == *start){
ptr->prev = temp;
ptr->next = temp->next;
temp->next->prev = ptr;
temp->next = ptr;
}
else{
ptr->next = temp->next;
ptr->prev = temp;
temp->next->prev = ptr;
temp->next = ptr;
}
}
}
Node insertion not possible as list is empty Empty list 24 24 4 24 4 4 24 4 4 4 Error: Invalid position 24 4 4 4
Comments
Post a Comment