doubly-linked-list-using-C-Data-Structure

admin

1/25/2025

#doubly-linked-list-using-C-Data-Structure

Go Back
Start learningdoubly-linked-list-using-C-Data-Structure

Doubly Linked List Algorithm - Data Structure

A doubly linked list is a versatile data structure that enhances the capabilities of a traditional singly linked list. In this structure, each node contains three parts: the data element, a pointer to the previous node, and a pointer to the next node in the sequence. This bidirectional linkage allows traversal in both directions, making it highly efficient for applications like undo functionality, navigation systems, and data manipulation.


Key Features of Doubly Linked List:

  1. Bidirectional Traversal: Nodes can be traversed both forward and backward.
  2. Dynamic Size: Nodes can be added or removed dynamically, ensuring efficient memory usage.
  3. Versatile Operations: Allows operations such as insertion, deletion, and traversal at both ends with ease.

Example Implementation of Doubly Linked List in C

Below is an example of a doubly linked list implementation in C. It covers key operations like appending nodes, traversing in forward and backward directions, searching for nodes, updating values, and inserting/deleting nodes based on specific criteria.

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

struct node {
    int no;
    struct node *next, *pre;
};

struct node *start = NULL, *last = NULL;

// Append a node
void append() {
    struct node *n;
    n = (struct node *)malloc(sizeof(struct node));
    printf("\n Enter No :");
    scanf("%d", &n->no);
    n->next = NULL;

    if (start == NULL) {
        start = last = n;
        start->pre = NULL;
        printf("\n Node Appended ");
        return;
    }
    last->next = n;
    n->pre = last;
    last = n;
    printf("\n Node Appended ");
}

// Forward traversal
void ftraverse() {
    struct node *p = start;
    if (start == NULL) {
        printf("\n Empty Linked List ");
        return;
    }
    printf("\n START ");
    while (p != NULL) {
        printf(" %d", p->no);
        p = p->next;
    }
    printf(" END\n");
}

// Backward traversal
void btraverse() {
    struct node *p = last;
    if (last == NULL) {
        printf("\n Empty Linked List ");
        return;
    }
    printf("\n LAST ");
    while (p != NULL) {
        printf(" %d", p->no);
        p = p->pre;
    }
    printf(" START\n");
}

// Main function
int main() {
    int ch;
    do {
        printf("\n1 Append\n2 Forward Traversal\n3 Backward Traversal\n0 Exit\nEnter Choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1: append(); break;
            case 2: ftraverse(); break;
            case 3: btraverse(); break;
            case 0: break;
            default: printf("\n Invalid Choice\n");
        }
    } while (ch != 0);

    return 0;
}

Key Operations of Doubly Linked List:

  1. Insertion:
    • Insert at the beginning.
    • Insert at the end.
    • Insert after a specific node.
    • Insert before a specific node.
  2. Deletion:
    • Delete by value.
    • Delete by position.
  3. Traversal:
    • Forward traversal.
    • Backward traversal.
  4. Search:
    • Locate a node based on its value.
  5. Update:
    • Modify the value of an existing node.

Use Cases of Doubly Linked List

  • Navigation Systems: Maintaining a history of visited pages or locations.
  • Undo/Redo Operations: In text editors or graphic design software.
  • Deque Implementations: Supporting efficient insertion and deletion from both ends.

Benefits of Using a Doubly Linked List

  1. Efficient Traversal: Backward traversal is as easy as forward traversal.
  2. Flexible Node Manipulation: Easy to add or remove nodes dynamically.
  3. Improved Functionality: Offers more functionality compared to singly linked lists, at the cost of additional memory for storing the previous pointer.

SEO Analysis and Optimization

Target Keywords:

  • Doubly linked list
  • Data structure algorithm
  • Doubly linked list C implementation
  • Advantages of doubly linked list
  • Doubly linked list use cases

Long-tail Keywords:

  • "What is a doubly linked list in data structures?"
  • "How to implement a doubly linked list in C?"
  • "Advantages and disadvantages of a doubly linked list."
  • "Applications of doubly linked list in real-world systems."

Conclusion

The doubly linked list is an indispensable data structure that provides a balance between complexity and functionality. Its ability to traverse in both directions and perform efficient insertions and deletions makes it suitable for a wide range of applications. Learning and mastering this structure is essential for anyone looking to excel in data structure algorithms.

Pro Tip: Practice implementing a doubly linked list in different programming languages to understand its versatility and underlying logic better.