Lightweight implementation of queues in TypeScript.
You can use it to improve the performance of your node or browser applications built with JavaScript/TypeScript
This package contains six different implementations of queue:
- Array queue (
new ArrayQueue()
) - Array stack queue (
new ArrayStackQueue()
) - Singly linked list stack queue (
new SinglyLinkedListStackQueue()
) - Doubly linked list stack queue (
new DoublyLinkedListStackQueue()
) - Circular singly linked list stack queue (
new CircularSinglyLinkedListStackQueue()
) - Circular doubly linked list stack queue (
new CircularDoublyLinkedListStackQueue()
)
All queues contains similar properties and methods.
Here is what each class contains:
In all examples below, we used ArrayQueue implementation. But the usages are just the same for all implementations.
Converts the queue into an array
const queue = new ArrayQueue()
const array = queue.add(1).add(2).add(3).toArray()
// [1, 2, 3]
Adds an element to the end of the queue. Returns the queue object for chaining.
const queue = new ArrayQueue()
const array = queue.add(1).add(2).add(3).toArray()
// [1, 2, 3]
Adds an element to the end of the queue. Returns true to indicate the element was added successfully.
const queue = new ArrayQueue()
const result = queue.offer(1).offer(2).offer(3)
// true
Removes and returns the element from the front of the queue. Returns null if the queue is empty.
const queue = new ArrayQueue()
const result = queue.add(1).add(2).poll()
// 1
Removes and returns the element from the front of the queue. Throws an error if the queue is empty.
const queue = new ArrayQueue()
const result = queue.add(1).add(2).remove()
// 1
Returns the element at the front of the queue without removing it. Returns null if the queue is empty.
const queue = new ArrayQueue()
const result = queue.add(1).add(2).peek()
// 1
Returns the element at the front of the queue without removing it. Throws an error if the queue is empty.
const queue = new ArrayQueue()
const result = queue.add(1).add(2).element()
// 1
Returns true if the queue is empty
const queue = new ArrayQueue()
const result = queue.isEmpty()
// true
Removes all elements from the queue
const queue = new ArrayQueue()
const result = queue.add(1).add(2).clear().isEmpty()
// true
Reverses elements inside queue
const queue = new ArrayQueue()
const result = queue.add(1).add(2).reverse().toArray()
// [2, 1]