Operations in a double linked list

Here are some common operations that can be performed on a double linked list in C:


1. Creating a double linked list:
```c
struct node {
    int data;
    struct node *next;
    struct node *prev;
};

struct node *head = NULL;
struct node *tail = NULL;
```

2. Inserting a node at the beginning of the list:
```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;
    }
}
```

3. Inserting a node at the end of the list:
```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;
    }
}
```

4. Inserting a node after a given 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;
}
```

5. Deleting a node from the beginning of the list:
```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);
}
```

6. Deleting a node from the end of the list:
```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);
}
```

7. Deleting a node with a given value:
```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);
}
```

8. Displaying the contents of the list


About the Author



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





 PreviousNext