Here are some common operations that can be performed on a double linked list in C:
```c
struct node {
int data;
struct node *next;
struct node *prev;
};
struct node *head = NULL;
struct node *tail = NULL;
```
```c
void insert_at_beginning(int value) {
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = value;
new_node->next = head;
new_node->prev = NULL;
if (head == NULL) {
head = tail = new_node;
} else {
head->prev = new_node;
head = new_node;
}
}
```
```c
void insert_at_end(int value) {
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = value;
new_node->next = NULL;
new_node->prev = tail;
if (head == NULL) {
head = tail = new_node;
} else {
tail->next = new_node;
tail = new_node;
}
}
```
```c
void insert_after_node(struct node *prev_node, int value) {
if (prev_node == NULL) {
printf("Previous node cannot be NULL.\n");
return;
}
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = value;
new_node->next = prev_node->next;
new_node->prev = prev_node;
if (prev_node->next != NULL) {
prev_node->next->prev = new_node;
} else {
tail = new_node;
}
prev_node->next = new_node;
}
```
```c
void delete_from_beginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct node *temp = head;
head = head->next;
if (head == NULL) {
tail = NULL;
} else {
head->prev = NULL;
}
free(temp);
}
```
```c
void delete_from_end() {
if (tail == NULL) {
printf("List is empty.\n");
return;
}
struct node *temp = tail;
tail = tail->prev;
if (tail == NULL) {
head = NULL;
} else {
tail->next = NULL;
}
free(temp);
}
```
```c
void delete_node(int value) {
struct node *temp = head;
while (temp != NULL && temp->data != value) {
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found in list.\n");
return;
}
if (temp == head) {
head = head->next;
} else {
temp->prev->next = temp->next;
}
if (temp == tail) {
tail = tail->prev;
} else {
temp->next->prev = temp->prev;
}
free(temp);
}
```
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc