0
0
CHow-ToBeginner · 4 min read

How to Create a Doubly Linked List in C: Syntax and Example

To create a doubly linked list in C, define a struct with pointers to both the previous and next nodes. Then, dynamically allocate nodes and link them by setting these pointers to connect nodes in both directions.
📐

Syntax

A doubly linked list node in C is defined using a struct that contains three parts:

  • data: stores the value (e.g., an integer)
  • prev: pointer to the previous node
  • next: pointer to the next node

This allows traversal forward and backward through the list.

c
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;
💻

Example

This example creates a doubly linked list with three nodes, links them, and prints the list forward and backward.

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

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

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

int main() {
    // Create nodes
    Node* first = createNode(10);
    Node* second = createNode(20);
    Node* third = createNode(30);

    // Link nodes forward
    first->next = second;
    second->prev = first;
    second->next = third;
    third->prev = second;

    // Print list forward
    printf("Forward: ");
    Node* temp = first;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    // Print list backward
    printf("Backward: ");
    temp = third;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->prev;
    }
    printf("\n");

    // Free memory
    free(first);
    free(second);
    free(third);

    return 0;
}
Output
Forward: 10 20 30 Backward: 30 20 10
⚠️

Common Pitfalls

Common mistakes when creating doubly linked lists include:

  • Not initializing prev and next pointers to NULL, which can cause undefined behavior.
  • Forgetting to update both prev and next pointers when inserting or deleting nodes.
  • Not freeing all allocated memory, leading to memory leaks.

Always carefully link nodes in both directions and check for NULL pointers when traversing.

c
/* Wrong way: Only linking next pointer */
first->next = second;
// second->prev is not set, breaks backward traversal

/* Right way: Link both pointers */
first->next = second;
second->prev = first;
📊

Quick Reference

  • Define node with prev and next pointers.
  • Initialize pointers to NULL when creating nodes.
  • Link nodes by setting both prev and next.
  • Traverse forward using next, backward using prev.
  • Free all nodes to avoid memory leaks.

Key Takeaways

A doubly linked list node has pointers to both previous and next nodes for two-way traversal.
Always initialize prev and next pointers to NULL when creating nodes.
Update both prev and next pointers when linking nodes to maintain list integrity.
Traverse the list forward with next and backward with prev pointers.
Free all allocated nodes to prevent memory leaks.