DEV Community

MD. Amran
MD. Amran

Posted on

Understanding Singly and Doubly Linked Lists with Real-Life Examples and JavaScript Code

Introduction
Linked lists are one of the fundamental data structures used in computer science. They are dynamic, allowing efficient insertions and deletions. In this post, we’ll explore two types: Singly Linked Lists and Doubly Linked Lists, with real-life examples and JavaScript implementations.

1️⃣ Singly Linked List
A Singly Linked List (SLL) consists of nodes where each node contains:

  • A value (data)
  • A next pointer pointing to the next node

However, it does not have a reference to the previous node, so traversal is one-directional.

πŸ”Ή Real-Life Example
Train with one-way connectivity
Imagine a train where each coach is connected only to the next one. You can move forward but not backward.

Other examples:

  • Music Playlist (Next song only)
  • Call Logs on Phones

πŸ”Ή JavaScript Implementation

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class SinglyLinkedList {
  constructor() {
    this.head = null;
  }

  // Insert at the end
  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  // Print the list
  print() {
    let current = this.head;
    let list = "";
    while (current) {
      list += current.value + " -> ";
      current = current.next;
    }
    console.log(list + "null");
  }
}

// Example usage:
const sll = new SinglyLinkedList();
sll.append(10);
sll.append(20);
sll.append(30);
sll.print(); // Output: 10 -> 20 -> 30 -> null
Enter fullscreen mode Exit fullscreen mode

2️⃣ Doubly Linked List
A Doubly Linked List (DLL) consists of nodes where each node contains:

  • A value (data)
  • A next pointer pointing to the next node
  • A previous pointer pointing to the previous node

This allows traversal in both directions.

πŸ”Ή Real-Life Example
Web Browser History
When you navigate in a browser, you can go forward and backward smoothly because each page is linked to both the previous and next page.

Other examples:

  • Music Playlist (Next & Previous buttons)
  • Undo/Redo in Software

πŸ”Ή JavaScript Implementation

class DNode {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  // Insert at the end
  append(value) {
    const newNode = new DNode(value);
    if (!this.head) {
      this.head = this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  // Print forward
  printForward() {
    let current = this.head;
    let list = "";
    while (current) {
      list += current.value + " <-> ";
      current = current.next;
    }
    console.log(list + "null");
  }

  // Print backward
  printBackward() {
    let current = this.tail;
    let list = "";
    while (current) {
      list += current.value + " <-> ";
      current = current.prev;
    }
    console.log(list + "null");
  }
}

// Example usage:
const dll = new DoublyLinkedList();
dll.append(10);
dll.append(20);
dll.append(30);
dll.printForward(); // Output: 10 <-> 20 <-> 30 <-> null
dll.printBackward(); // Output: 30 <-> 20 <-> 10 <-> null
Enter fullscreen mode Exit fullscreen mode

πŸš€ Key Differences
| Feature | Singly Linked List | Doubly Linked List |
|----------------------|------------------|------------------|
| Direction | Forward only | Forward & Backward |
| Memory Usage | Less (1 pointer) | More (2 pointers) |
| Traversal | One-way | Two-way |
| Reverse Traversal | ❌ Not possible | βœ… Possible |
| Use Case Examples | Train (One-way), Call logs | Browser history, Undo/Redo |


Conclusion

  • Singly Linked Lists are more memory-efficient and simpler.
  • Doubly Linked Lists offer more flexibility for traversing in both directions.
  • Choosing the right linked list depends on the specific use case.

Hope this guide helps you understand linked lists better! πŸš€ Feel free to drop any questions or suggestions below. Happy coding! πŸ’»

Image description

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay