0 0
Read Time:3 Minute, 16 Second

Codes for Insertion In Doubly Liked List

C Code for Inserting a node at the start of a Doubly linked list

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

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

struct Node* head;

void insertAtStart(int value) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = value;
    new_node->prev = NULL;
    new_node->next = head;
    if (head != NULL) {
        head->prev = new_node;
    }
    head = new_node;
}

int main() {
    head = NULL;
    insertAtStart(10);
    insertAtStart(20);
    insertAtStart(30);
    insertAtStart(40);
    return 0;
}

In this code, we have defined a struct Node which contains the data and the pointers to the previous and next nodes in the list. We have also defined a global variable head which points to the first node in the list.

The insertAtStart function takes an integer value as input and creates a new node with that value. It sets the new node’s prev pointer to NULL and next pointer to the current head. It then sets the prev pointer of the current head (if it exists) to the new node and finally sets the head pointer to the new node, making it the new first element in the list.

In the main function, we initialize the head pointer to NULL and call the insertAtStart function multiple times to insert new nodes with the values 10, 20, 30, and 40 at the start of the list.

code for inserting a node at the end position in a doubly linked list

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

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

// Function to create a new node at the end
void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->prev = NULL;
    new_node->next = (*head_ref);
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
    (*head_ref) = new_node;
}

// Function to insert a new node after a given node
void insertAfter(struct Node* prev_node, int new_data) {
    if (prev_node == NULL) {
        printf("the given previous node cannot be NULL");
        return;
    }
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = prev_node->next;
    prev_node->next = new_node;
    new_node->prev = prev_node;
    if (new_node->next != NULL)
        new_node->next->prev = new_node;
}

// Function to insert a new node at the end
void append(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *head_ref;
    new_node->data = new_data;
    new_node->next = NULL;
    if (*head_ref == NULL) {
        new_node->prev = NULL;
        *head_ref = new_node;
        return;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    new_node->prev = last;
    return;
}

// Function to print the doubly linked list
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main() {
    struct Node* head = NULL;
    append(&head, 6);
    push(&head, 7);
    push(&head, 1);
    append(&head, 4);
    insertAfter(head->next, 8);
    printf("Created DLL is: ");
    printList(head);
    return 0;
}

In this code, we define a structure called Node, which contains an integer data and two pointers, prev and next, which point to the previous and next nodes respectively. The push() function creates a new node at the beginning of the doubly linked list, the append() function creates a new node at the end, and the insertAfter() function creates a new node after a given node. In the main function, we call the append(), push(), and insertAfter() function to insert new nodes into the doubly linked list and the printList() function to display the doubly linked list.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *