@jiminp/tooltool
    Preparing search index...

    Class Deque<T>

    Represents a double-ended queue (deque) with amortized O(1) insertion and removal at both ends. The implementation stores items inside a sparse index map so the head and tail can grow independently without costly array shifts.

    const deque = new Deque<number>();
    deque.push(1, 2);
    deque.unshift(0);
    deque.pop(); // 2

    Type Parameters

    • T

      Type of item stored inside the deque.

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Type Parameters

      • T

        Type of item stored inside the deque.

      Returns Deque<T>

    Accessors

    • get length(): number

      Current number of items held by the deque.

      Returns number

      const deque = new Deque<string>();
      deque.push("a");
      deque.length; // 1

    Methods

    • Retrieves the item at the provided position.

      An index of -1 returns the tail item, 0 returns the head, and any non-integer input is truncated toward zero.

      Parameters

      • index: number

        Requested position inside the deque.

      Returns T | null

      The referenced item or null when the position is out of range.

      const deque = new Deque<number>();
      deque.push(10, 20, 30);
      deque.at(-1); // 30
    • Removes and returns the item at the tail of the deque.

      Returns T | null

      The removed item, or null if the deque is empty.

      const deque = new Deque<number>();
      deque.push(1, 2);
      deque.pop(); // 2
    • Appends items to the tail of the deque.

      Parameters

      • ...items: T[]

        Items to append, in the order they should appear.

      Returns number

      The updated deque length.

      const deque = new Deque<number>();
      deque.push(1, 2); // 2
    • Removes and returns the item at the head of the deque.

      Returns T | null

      The removed item, or null if the deque is empty.

      const deque = new Deque<number>();
      deque.push(1, 2);
      deque.shift(); // 1
    • Inserts items at the head of the deque.

      Parameters

      • ...items: T[]

        Items to insert, where the first argument becomes the new head.

      Returns number

      The updated deque length.

      const deque = new Deque<number>();
      deque.unshift(2, 3); // 2
      deque.unshift(1); // 3