Lightweight implementation of stacks in TypeScript.
You can use it to improve the performance of your node or browser applications built with JavaScript/TypeScript
This package contains five different implementations of stack:
- Array stack (
new ArrayStack()
) - Singly linked list stack (
new SinglyLinkedListStack()
) - Doubly linked list stack (
new DoublyLinkedListStack()
) - Circular singly linked list stack (
new CircularSinglyLinkedListStack()
) - Circular doubly linked list stack (
new CircularDoublyLinkedListStack()
)
All stacks contains similar properties and methods.
Here is what each class contains:
In all examples below, we used ArrayStack implementation. But the usages are just the same for all implementations.
Converts the stack into an array
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).toArray()
// [1, 2, 3]
Pushes a new item to the stack
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).toArray()
// [1, 2, 3]
Removes the most top item from the stack and returns it
const stack = new ArrayStack()
const topItem = stack.push(1).push(2).push(3).pop()
// 3
Returns the most top item from the stack
const stack = new ArrayStack()
const topItem = stack.push(1).push(2).push(3).peek()
// 3
Removes all items from the stack
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).clear().toArray()
// []
Checks if the stack is empty
const stack = new ArrayStack()
const isEmpty = stack.isEmpty()
// true