lifoer
v0.0.1
Published
An implementation of stacks in TypeScript for nodejs and browser
Readme
Lifoer
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.
.toArray<T>(): T[]
Converts the stack into an array
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).toArray()
// [1, 2, 3].push<T>(value: T): this
Pushes a new item to the stack
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).toArray()
// [1, 2, 3].pop<T>(): T | null
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.peek<T>(): T | null
Returns the most top item from the stack
const stack = new ArrayStack()
const topItem = stack.push(1).push(2).push(3).peek()
// 3.clear<T>(): this
Removes all items from the stack
const stack = new ArrayStack()
const array = stack.push(1).push(2).push(3).clear().toArray()
// [].isEmpty<T>(): boolean
Checks if the stack is empty
const stack = new ArrayStack()
const isEmpty = stack.isEmpty()
// true