Remove data from Doubly Linked List

Implementation

Implementation of this algorithm is given below −

```#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *prev;
struct node *next;
};

struct node *last = NULL;
struct node *current = NULL;

//display the list
void printList() {

//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->next;
}

printf(" [last]\n");
}

void print_backward() {
struct node *ptr = last;

printf("\n[last] <=>");
//start from the beginning
while(ptr != NULL) {
printf(" %d <=>",ptr->data);
ptr = ptr->prev;
}

}

void insert(int data) {
// Allocate memory for new node;
struct node *link = (struct node*) malloc(sizeof(struct node));

// If head is empty, create new list
return;
}

// move to the end of the list
while(current->next!=NULL)
current = current->next;

// Insert link at the end of the list
}

void remove_data(int data) {
int pos = 0;
struct node *pre_node;

return;
}

return;
} else {
printf("List is empty now");
return;
}
return;
}

while(current->next != NULL && current->data != data) {
pre_node = current;
current = current->next;
}

if(current->data == data) {
pre_node->next = pre_node->next->next;

if(pre_node->next != NULL) {          // link back
pre_node->next->prev = pre_node;
} else
last = pre_node;

free(current);
} else

}

int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);

printList();
remove_data(20);

printList();
print_backward();

return 0;
}
```

Output

Output of the program should be −

```[head] <=> 10 <=> 20 <=> 30 <=> 1 <=> 40 <=> 56 <=> [last]

[head] <=> 10 <=> 30 <=> 1 <=> 40 <=> 56 <=> [last]

[last] <=> 56 <=> 40 <=> 1 <=> 30 <=> 10 <=> [head]
```