Data Structure: Mastering Linked Lists (Singly, Doubly, and Circular)

A linked list is a dynamic data structure that stores elements in non-contiguous memory. Unlike arrays, it grows/shrinks at runtime, making it ideal for unpredictable data volumes. Think of it as a chain of “nodes,” where each node holds data and a pointer to the next node.

Key Terminology

  • Node: Basic unit (contains data + pointer to previous or next node).
  • Head: Pointer to the first node.
  • Tail: Pointer to the last node (null in singly linked lists).
  • Pointer/Link: Address of the next node.
Doubly linked list structure

Node structure

/*
 * Basic structure of a node
 */
struct node {
    int data;           // Data (int, char, etc.)
    struct node* next;  // Pointer to next node
};

Core operations

  1. Creation
  2. Traversal
  3. Insertion
  4. Deletion
  5. Search

Why Linked Lists Over Arrays?

Arrays have limitations: fixed size, costly insertions/deletions, and memory wastage. Linked lists solve these:

  • Dynamic size: Allocates memory as needed.
  • Efficient operations: Inserts/deletes in O(1) time at head.
  • No memory waste: Uses only required space.

💡 Use linked lists when: You need frequent insertions/deletions or unknown data size.

Types of Linked Lists

  1. Singly Linked List:
    • Traverse in one direction (head → tail).
    • Operations: Insert/delete at start (O(1)), end (O(n)).
  2. Doubly Linked List:
    • Nodes point to next and previous nodes.
    • Enables bidirectional traversal.
    • Operations: Insert/delete at start/end (O(1)), other (O(n)).
  3. Circular Linked List:
    • Tail points back to head (loop).
    • Operations: Insert/delete at start/end (O(1)), other (O(n)).
    • Applications: OS scheduling, multiplayer games.
  4. Circular Doubly Linked List:
    • Combines circular + doubly linked features.

Pros & Cons

AdvantagesDisadvantages
Dynamic memory allocation (usage)No random access (e.g., arr[5])
Fast insertions/deletionsExtra memory for pointers
Flexible size

Real-World Applications (use)

  • Undo/Redo: Stored as node states (e.g., text editors).
  • Hash Tables: Collision handling via chaining.
  • Memory Management: OS memory block allocation.
  • Navigation: GPS paths (doubly linked lists).