Doubly Linked List

 

What is a Doubly Linked List (DLL)?

A doubly linked list is a type of linked list where each node has three parts:

  • Data (the value)

  • A pointer to the next node (next)

  • A pointer to the previous node (prev)

This allows movement in both directions — forward and backward.

Each node is like this:

[ prev | data | next ]



📘 Algorithms for Doubly Linked List

1. Insert at Beginning

Algorithm InsertAtBeginning(value) 1. Create a new node 2. Set newNode.data = value 3. Set newNode.prev = NULL 4. Set newNode.next = head 5. If head is not NULL head.prev = newNode 6. Set head = newNode

2. Insert at End

Algorithm InsertAtEnd(value)
1. Create a new node 2. Set newNode.data = value 3. Set newNode.next = NULL 4. If head is NULL head = newNode newNode.prev = NULL Else temp = head While temp.next != NULL temp = temp.next temp.next = newNode newNode.prev = temp

3. Insert at a Given Position

Algorithm InsertAtPosition(value, position)
1. Create a new node 2. If position == 1 Call InsertAtBeginning(value) Return 3. temp = head 4. Move temp to (position-1)th node 5. newNode.next = temp.next 6. If temp.next != NULL temp.next.prev = newNode 7. temp.next = newNode 8. newNode.prev = temp

4. Delete from Beginning

Algorithm DeleteAtBeginning()
1. If head == NULL Print "Empty list" Return 2. temp = head 3. head = head.next 4. If head != NULL head.prev = NULL 5. Free temp

5. Delete from End

Algorithm DeleteAtEnd()
1. If head == NULL Print "Empty list" Return 2. If head.next == NULL Free head head = NULL Return 3. temp = head 4. Move temp to last node 5. temp.prev.next = NULL 6. Free temp

6. Delete at Given Position

Algorithm DeleteAtPosition(position)
1. If head == NULL Print "Empty list" Return 2. If position == 1 Call DeleteAtBeginning() Return 3. temp = head 4. Move temp to (position)th node 5. temp.prev.next = temp.next 6. If temp.next != NULL temp.next.prev = temp.prev 7. Free temp

7. Traversal Forward

Algorithm TraverseForward()
1. temp = head 2. While temp != NULL Print temp.data temp = temp.next

8. Traversal Backward

(First move to last node, then back using prev)

Algorithm TraverseBackward() 1. temp = head 2. While temp.next != NULL temp = temp.next 3. While temp != NULL Print temp.data temp = temp.prev

📌 Important Notes

  • Always adjust both prev and next pointers carefully.

  • Check NULL conditions before dereferencing.

  • Free the nodes after deletion to avoid memory leaks.


 C program Doubly Linked List Operations

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

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

struct Node* head = NULL;

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(0);
    }
    newNode->data = value;
    newNode->next = NULL;
    newNode->prev = NULL;
    return newNode;
}

// Insert at beginning
void insertAtBeginning(int value) {
    struct Node* newNode = createNode(value);
    if (head == NULL) {
        head = newNode;
    } else {
        newNode->next = head;
        head->prev = newNode;
        head = newNode;
    }
    printf("%d inserted at beginning\n", value);
}

// Insert at end
void insertAtEnd(int value) {
    struct Node* newNode = createNode(value);
    if (head == NULL) {
        head = newNode;
    } else {
        struct Node* temp = head;
        while (temp->next != NULL)
            temp = temp->next;
        temp->next = newNode;
        newNode->prev = temp;
    }
    printf("%d inserted at end\n", value);
}

// Insert at given position
void insertAtPosition(int value, int position) {
    if (position == 1) {
        insertAtBeginning(value);
        return;
    }
    struct Node* newNode = createNode(value);
    struct Node* temp = head;
    int i;
    for (i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Position out of bounds\n");
        free(newNode);
        return;
    }
    newNode->next = temp->next;
    if (temp->next != NULL)
        temp->next->prev = newNode;
    temp->next = newNode;
    newNode->prev = temp;
    printf("%d inserted at position %d\n", value, position);
}

// Delete at beginning
void deleteAtBeginning() {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* temp = head;
    head = head->next;
    if (head != NULL)
        head->prev = NULL;
    printf("Deleted %d from beginning\n", temp->data);
    free(temp);
}

// Delete at end
void deleteAtEnd() {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* temp = head;
    if (temp->next == NULL) {
        printf("Deleted %d from end\n", temp->data);
        free(temp);
        head = NULL;
        return;
    }
    while (temp->next != NULL)
        temp = temp->next;
    temp->prev->next = NULL;
    printf("Deleted %d from end\n", temp->data);
    free(temp);
}

// Delete at given position
void deleteAtPosition(int position) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    if (position == 1) {
        deleteAtBeginning();
        return;
    }
    struct Node* temp = head;
    int i;
    for (i = 1; i < position && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Position out of bounds\n");
        return;
    }
    if (temp->next != NULL)
        temp->next->prev = temp->prev;
    if (temp->prev != NULL)
        temp->prev->next = temp->next;
    printf("Deleted %d from position %d\n", temp->data, position);
    free(temp);
}

// Traverse forward
void traverseForward() {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* temp = head;
    printf("List (forward): ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// Traverse backward
void traverseBackward() {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    struct Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;
    printf("List (backward): ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->prev;
    }
    printf("\n");
}

// Main function with menu
int main() {
    int choice, value, position;
    while (1) {
        printf("\n--- Doubly Linked List Operations ---\n");
        printf("1. Insert at Beginning\n");
        printf("2. Insert at End\n");
        printf("3. Insert at Position\n");
        printf("4. Delete at Beginning\n");
        printf("5. Delete at End\n");
        printf("6. Delete at Position\n");
        printf("7. Traverse Forward\n");
        printf("8. Traverse Backward\n");
        printf("9. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertAtBeginning(value);
                break;
            case 2:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertAtEnd(value);
                break;
            case 3:
                printf("Enter value and position to insert: ");
                scanf("%d%d", &value, &position);
                insertAtPosition(value, position);
                break;
            case 4:
                deleteAtBeginning();
                break;
            case 5:
                deleteAtEnd();
                break;
            case 6:
                printf("Enter position to delete: ");
                scanf("%d", &position);
                deleteAtPosition(position);
                break;
            case 7:
                traverseForward();
                break;
            case 8:
                traverseBackward();
                break;
            case 9:
                printf("Exiting program\n");
                exit(0);
            default:
                printf("Invalid choice! Try again.\n");
        }
    }
    return 0;
}


Comments

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Sparse Matrix - Transpose and Addition

Polynomial Addition using Arrays