Now we will see how to delete node at position 'N' in doubly link list.
Algorithm to delete node at position 'N' in doubly link list:
In this algorithm, START points to last node of the link list and POS is position where the new node to be deleted. The steps are as follows:- Initialize TEMP and TEMP2
- If list have no node or position is less than one (TAIL == NULL or POS < 1)
- Invalid deletion
- Else if POS == 1
- insertAtFirst()
- Else
- TEMP = START
- Traverse the list so that TEMP points to POS-1 th node
- If TEMP->NEXT->NEXT == NULL
- TEMP->NEXT = NULL
- Else
- TEMP->NEXT = TEMP->NEXT->NEXT
- TEMP->NEXT->PREV = TEMP
- End If
- End If
Function to delete node at position 'N' in doubly link list:
int deleteAtPosition(NODE **start, int pos){
NODE *temp, *temp2 = *start;
int info = -1;
if(pos < 1 || *start == NULL){
printf("\nError : Invalid position");
return -1;
}
if(pos == 1){
info = (*start)->info;
if((*start)->next == NULL){
*start = NULL;
}
else{
*start = (*start)->next;
(*start)->prev = NULL;
}
free(temp2);
}
else{
temp = *start;
for(int i = 2; i < pos; i++){
temp = temp->next;
if(temp->next == NULL){
printf("\nError: Invalid position\n");
return -1;
}
}
temp2 = temp->next;
info = temp2->info;
if(temp->next->next == NULL){
temp->next = NULL;
}
else{
temp->next = temp->next->next;
temp->next->prev = temp;
}
free(temp2);
}
return info;
}
Program to delete node at position 'N' in doubly link list:
#include <stdio.h>
#include <malloc.h>
typedef struct node{
int info;
struct node *next, *prev;
} NODE;
void insertAtLast(NODE **, int);
int deleteAtPosition(NODE **, int);
void traverse(NODE **);
int main(){
NODE *start = NULL;
insertAtLast(&start, 1);
insertAtLast(&start, 2);
insertAtLast(&start, 3);
insertAtLast(&start, 4);
insertAtLast(&start, 5);
deleteAtPosition(&start, 1);
traverse(&start);
deleteAtPosition(&start, 2);
traverse(&start);
deleteAtPosition(&start, 3);
traverse(&start);
deleteAtPosition(&start, 3);
traverse(&start);
return 0;
}
int deleteAtPosition(NODE **start, int pos){
NODE *temp, *temp2 = *start;
int info = -1;
if(pos < 1 || *start == NULL){
printf("\nError : Invalid position");
return -1;
}
if(pos == 1){
info = (*start)->info;
if((*start)->next == NULL){
*start = NULL;
}
else{
*start = (*start)->next;
(*start)->prev = NULL;
}
free(temp2);
}
else{
temp = *start;
for(int i = 2; i < pos; i++){
temp = temp->next;
if(temp->next == NULL){
printf("\nError: Invalid position\n");
return -1;
}
}
temp2 = temp->next;
info = temp2->info;
if(temp->next->next == NULL){
temp->next = NULL;
}
else{
temp->next = temp->next->next;
temp->next->prev = temp;
}
free(temp2);
}
return info;
}
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");
}
Output:2 3 4 5 2 4 5 2 4 Error: Invalid position 2 4
Comments
Post a Comment