Collection Kit

DoublyLinkedList

Linear

A DoublyLinkedList is a linear data structure where each node contains a reference to both the next and previous nodes, allowing traversal in both directions.

Import Statement
import { DoublyLinkedList } from "collection-kit";

Key Features

  • Bidirectional Traversal: Can be traversed in both forward and backward directions.
  • Efficient Insertions/Deletions: O(1) time complexity for adding or removing elements at both ends.

Common Operations

Add
Insert an element at the beginning, end, or a specific index.
Remove
Delete an element by value or index.
Get
Retrieve an element at a specific index.
Size
Return the number of elements in the list.

Example Code

import { DoublyLinkedList } from "collection-kit";

const list = new DoublyLinkedList();
list.add(10);
list.add(20);
list.add(30);

console.log("Size:", list.size());         // 3
console.log("First element:", list.get(0)); // 10
console.log("Last element:", list.get(2));  // 30