Algorithms and data structures for JS/TS
  • Introduction
  • GETTING STARTED
    • TS
    • Node.js
    • ES Modules
    • Browsers
  • PUBLIC API
    • How to read
    • Linked lists
      • SinglyLinkedList
      • DoublyLinkedList
      • CircularlyLinkedList
    • Stacks and queues
      • LinkedStack
      • LinkedQueue
      • LinkedDeque
      • CircularQueue
      • CircularArrayBuffer
      • UnsortedPriorityQueue
      • SortedPriorityQueue
      • AdaptableHeapPriorityQueue
    • Maps
      • SortedMap
      • MaximaSet
      • AVLTreeMap
      • SplayTreeMap
      • RedBlackTreeMap
    • Trees
      • GeneralTree
      • LinkedBinaryTree
      • PreorderTreeTraversal
      • InorderTreeTraversal
      • PostorderTreeTraversal
      • EulerTourTreeTraversal
    • Searches
      • Binary search
      • Quick select
    • Text processing
      • Longest common subsequence
      • Boyer-Moore
      • Knuth-Morris-Pratt
    • Position
    • Locator
    • Comparators
  • CONTRIBUTION NOTES
    • How to contribute
    • Project structure
  • Changelog
Powered by GitBook
On this page
  • new LinkedDeque()
  • length
  • clear()
  • dequeue()
  • dequeLast()
  • enqueue()
  • enqueueFirst()
  • getFirst()
  • getLast()
  • isEmpty()

Was this helpful?

  1. PUBLIC API
  2. Stacks and queues

LinkedDeque

Container of elements that are inserted and removed on either side. This structure is based on DoublyLinkedList.

new LinkedDeque()

/**
 * Creates an instance of LinkedDeque.
 *
 * @param elements List of elements to create the new queue with.
 */
constructor(elements: T[] = [])

length

length: number

Number of elements in the queue. This field is read only.

Examples:

import { LinkedDeque } from 'ads-js/queues';

const queue = new LinkedDeque();

queue.length === 0; // true
queue.enqueue(1);
queue.length === 1; // true

clear()

/**
 * Clears the queue.
 */
clear(): void;

dequeue()

/**
 * Removes the first element from the front of the queue and returns it.
 * Throws an error if the queue is empty.
 *
 * @returns Removed element.
 */
dequeue(): T;

dequeLast()

/**
 * Removes the last element from the rear of the queue and returns it. Throws an error if the queue is empty.
 *
 * @returns Removed element.
 */
dequeueLast(): T;

enqueue()

/**
 * Adds element at the rear of the queue.
 *
 * @param element Element to add.
 */
enqueue(element: T): void;

enqueueFirst()

/**
 * Adds element at the front of the queue.
 *
 * @param element Element to add.
 */
enqueueFirst(element: T): void;

getFirst()

/**
 * Gets element from the front of the queue without its removal.
 *
 * @returns Queue element.
 */
getFirst(): T;

getLast()

/**
 * Gets element from the rear of the queue without its removal.
 *
 * @returns Queue element.
 */
getLast(): T;

isEmpty()

isEmpty(): boolean

Running time O(1)

Checks whether the queue is empty or not.

Returns:

TRUE if the queue is empty, FALSE otherwise.

Examples:

import { LinkedDeque } from 'ads-js/queues';

const queue = new LinkedDeque();

queue.isEmpty(); // true
queue.enqueue(1);
queue.isEmpty(); // false
PreviousLinkedQueueNextCircularQueue

Last updated 5 years ago

Was this helpful?