A double linked list is a type of linked list where each node has two pointers, one to the next node in the list and one to the previous node in the list. This allows for more efficient traversal in both directions compared to a single linked list. Here's an example implementation of a double linked list in C:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
struct node *head = NULL;
struct node *tail = NULL;
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 = NULL;
if (head == NULL) {
head = tail = new_node;
} else {
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
}
void display() {
struct node *temp = head;
if (head == NULL) {
printf("List is empty.\n");
} else {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main() {
insert_at_end(10);
insert_at_end(20);
insert_at_end(30);
display();
return 0;
}
```
In this example, we define a `node` struct with `data`, `next`, and `prev` pointers. We also define `head` and `tail` pointers to keep track of the first and last nodes in the list, respectively.
The `insert_at_end` function inserts a new node with the given value at the end of the list. If the list is empty, the new node becomes the head and tail of the list. Otherwise, the new node is appended to the end of the list and its `prev` pointer is set to the previous tail.
The `display` function traverses the list from `head` to `tail`, printing the data of each node along the way.
In the `main` function, we insert three nodes into the list and display the contents of the list. The output of this program would be:
```
10 20 30
```
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