lib-stack-js
v1.0.1
Published
A small frontend utility library
Maintainers
Readme
lib-stack-js
A lightweight, zero-dependency JavaScript Stack implementation.
Perfect for learning data structures or using stacks in your projects.
📦 Installation
npm install lib-stack-js🚀 Usage
import { Stack } from 'lib-stack-js';
// Create a new stack
const stack = new Stack();
// Push elements
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.peek()); // 30 (last pushed item)
console.log(stack.pop()); // 30 (removed item)
console.log(stack.size()); // 2
console.log(stack.isEmpty()); // false
// Convert to array
console.log(stack.toArray()); // [10, 20]
// Clear the stack
stack.clear();
console.log(stack.isEmpty()); // true🛠 API
new Stack()
Creates a new empty stack.
.push(element)
Pushes an element onto the stack.
.pop()
Removes and returns the top element.
Throws an error if the stack is empty.
.peek()
Returns the top element without removing it.
Throws an error if the stack is empty.
.isEmpty()
Returns true if the stack is empty, else false.
.size()
Returns the number of elements in the stack.
.clear()
Empties the stack.
.toArray()
Returns a shallow copy of the stack as an array.
📜 License
MIT License
