CS With NehalCS With Nehal

Doubly Linked List Simulator

Visualise prev and next pointer changes when inserting, deleting and traversing a doubly linked list in both directions.

Operations

Each node stores a value and two pointers: prev and next.

Doubly linked list

Green = new node, orange = nodes whose pointers change, blue = visited.

head→
◀ null1020 ▶
⇄
◀ 102030 ▶
⇄
◀ 2030null ▶
←tail (30)

Each node: ◀ prev | value | next ▶

Drag to move · scroll to zoom

Compared with a singly linked list, each node needs extra memory for prev, but you can move both ways and delete a node without searching for the one before it.

Pseudocode

The current line is highlighted.

About This Simulation

This doubly linked list simulator shows how the prev and next pointers change when you insert, delete and traverse. Every pointer update is a separate step, so you can see exactly which links change and in what order.

Features

  • Insert at the head, the tail or a given position
  • Delete a node by value with both links updated
  • Forward traversal from the head and backward traversal from the tail
  • Each node shows its prev pointer, value and next pointer
  • Pseudocode with the active line highlighted

Topics covered

  • Doubly Linked List
  • Prev and Next Pointers
  • Head and Tail
  • Insertion and Deletion
  • Bidirectional Traversal

Explanation

In a doubly linked list each node stores a value and two pointers: next points to the following node and prev points to the one before. The list keeps a head pointer to the first node and a tail pointer to the last, so it can be walked in either direction.

How it works

  1. Insert at the head: the new node's next points to the old head, the old head's prev points back to the new node, then head moves to the new node.
  2. Insert at the tail: the new node's prev points to the old tail, the old tail's next points to the new node, then tail moves.
  3. Insert in the middle changes four pointers: the new node's prev and next, the previous node's next and the following node's prev.
  4. Delete a node by pointing its previous node's next past it and its next node's prev back past it, updating head or tail at the ends.
  5. Traverse forwards by following next from the head, or backwards by following prev from the tail.

Why it matters

Doubly linked lists are used for browser history, undo/redo, music playlists and the LRU caches in operating systems. They are a core topic in data structures courses such as CSE220.

Frequently Asked Questions

What is the difference between a singly and a doubly linked list?

A singly linked list node only points to the next node. A doubly linked list node also points to the previous node, so the list can be traversed in both directions.

Why use a doubly linked list?

Given a node, you can delete it or insert before it in O(1) time, because you can reach the node before it directly through prev.

What is the cost of a doubly linked list?

Every node needs extra memory for the prev pointer, and every insertion and deletion must update more pointers.

Related Simulations