es-stl - v3.0.2
    Preparing search index...

    First-in, first-out queue.

    The constructor reverses the provided initial array in place so the first input element can be dequeued first. Use a copied array when the original order must be preserved outside the queue.

    const queue = new Queue<string>();
    queue.enqueue('first').enqueue('second');

    queue.dequeue();
    // => 'first'

    Type Parameters

    • T

      Value type stored in the queue.

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Creates a queue from an optional initial array.

      Type Parameters

      • T

        Value type stored in the queue.

      Parameters

      • arr: T[] = []

        Initial values in dequeue order.

      Returns Queue<T>

      const queue = new Queue(['a', 'b']);

      queue.first();
      // => 'a'

    Properties

    _push: T[]
    _shift: T[]
    _size: number

    Accessors

    • get size(): number

      Number of values currently stored in the queue.

      Returns number

      The queue size.

      const queue = new Queue([1, 2, 3]);

      queue.size;
      // => 3

    Methods

    • Removes all values from the queue.

      Returns void

      const queue = new Queue([1, 2]);
      queue.clear();

      queue.size;
      // => 0
    • Removes and returns the value at the front of the queue.

      Returns T | undefined

      Front value, or undefined when the queue is empty.

      const queue = new Queue(['a', 'b']);

      queue.dequeue();
      // => 'a'

      queue.dequeue();
      // => 'b'
    • Adds a value to the back of the queue.

      Parameters

      • el: T

        Value to enqueue.

      Returns this

      This queue instance.

      const queue = new Queue<number>();

      queue.enqueue(1).enqueue(2);
      queue.last();
      // => 2
    • Reads the value at the front of the queue without removing it.

      Returns T | undefined

      Front value, or undefined when the queue is empty.

      const queue = new Queue(['a', 'b']);

      queue.first();
      // => 'a'

      queue.size;
      // => 2
    • Checks whether the queue contains no values.

      Returns boolean

      true when the queue is empty.

      new Queue().isEmpty();
      // => true
    • Reads the value at the back of the queue without removing it.

      Returns T | undefined

      Back value, or undefined when the queue is empty.

      const queue = new Queue(['a']);
      queue.enqueue('b');

      queue.last();
      // => 'b'