Now we will see how to insert a new node at position N in doubly link list.
Algorithm for insertion at position N in 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
- If list have no node i.e. START == NULL
- Set START = PTR
- Set PTR->NEXT = NULL
- Set PTR->PREV = NULL
- Else
- Set PTR->PREV = NULL
- Set PTR->NEXT = START
- Set START->PREV = PTR
- Set START = PTR
- End If;
- If list have no node i.e. START == NULL
- Else
- Traverse the list for (POS-1)th node into TEMP pointer
- If TEMP is last node i.e. TEMP->NEXT == NULL
- Set PTR->NEXT = NULL
- Set TEMP->PREV = TEMP
- Set TEMP->NEXT = PTR
- Else
- Set TEMP->NEXT->PREV = PTR
- Set PTR->NEXT = TEMP->NEXT
- Set PTR->PREV = TEMP
- Set TEMP->NEXT = PTR
- End If;
- End If;
Function to insert node at position N in 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\n");
free(ptr);
return;
}
if(pos == 1){
if(*start == NULL){
*start = ptr;
ptr->next = ptr->prev = NULL;
}
else{
ptr->prev = NULL;
ptr->next = *start;
(*start)->prev = ptr;
*start = ptr;
}
}
else{
temp = *start;
for(int i = 2; i < pos; i++){
temp = temp->next;
if(temp == NULL){
printf("\nError: Invalid position\n");
free(ptr);
return;
}
}
if(temp->next == NULL){
ptr->next = NULL;
temp->prev = temp;
temp->next = ptr;
}
else{
temp->next->prev = ptr;
ptr->next = temp->next;
ptr->prev = temp;
temp->next = ptr;
}
}
}
Program to insert at position N in the doubly link list:
#include <stdio.h>
#include <malloc.h>
typedef struct node{
int info;
struct node *next, *prev;
} NODE;
void insertAtLast(NODE **, int);
void insertAtPosition(NODE **, int, int);
void traverse(NODE **);
int main(){
NODE *start = NULL;
insertAtLast(&start, 1);
insertAtLast(&start, 2);
insertAtLast(&start, 3);
traverse(&start);
insertAtPosition(&start, 4, 0);
traverse(&start);
insertAtPosition(&start, 7, 1);
traverse(&start);
insertAtPosition(&start, 9, 3);
traverse(&start);
insertAtPosition(&start, 33, 6);
traverse(&start);
insertAtPosition(&start, 77, 8);
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\n");
free(ptr);
return;
}
if(pos == 1){
if(*start == NULL){
*start = ptr;
ptr->next = ptr->prev = NULL;
}
else{
ptr->prev = NULL;
ptr->next = *start;
(*start)->prev = ptr;
*start = ptr;
}
}
else{
temp = *start;
for(int i = 2; i < pos; i++){
temp = temp->next;
if(temp == NULL){
printf("\nError: Invalid position\n");
free(ptr);
return;
}
}
if(temp->next == NULL){
ptr->next = NULL;
temp->prev = temp;
temp->next = ptr;
}
else{
temp->next->prev = ptr;
ptr->next = temp->next;
ptr->prev = temp;
temp->next = ptr;
}
}
}
void insertAtLast(NODE **start, int info){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
NODE *temp = *start;
ptr->info = info;
ptr->next = NULL;
if(*start == NULL){
*start = ptr;
ptr->prev = NULL;
}
else{
while(temp->next != NULL){
temp = temp->next;
}
ptr->prev = temp;
temp->next = ptr;
}
}
void traverse(NODE **start){
NODE *temp = *start;
while(temp != NULL){
printf("%d ", temp->info);
temp = temp->next;
}
printf("\n");
}
1 2 3 Error : Invalid position 1 2 3 7 1 2 3 7 1 9 2 3 7 1 9 2 3 33 Error: Invalid position 7 1 9 2 3 33
Comments
Post a Comment