@utilityjs/stack
v2.0.0
Published
An implementation of Stack data structure.
Maintainers
Readme
UtilityJS | Stack
A Last-In-First-Out (LIFO) stack data structure.
Features
- LIFO Operations: Push and pop from the top
- Peek Support: View top element without removal
- Array Conversion: Convert stack to array
- TypeScript Support: Full generic type safety
Installation
npm install @utilityjs/stackor
pnpm add @utilityjs/stackUsage
import { Stack } from "@utilityjs/stack";
const stack = new Stack<number>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.peek(); // 3
stack.pop(); // 3
stack.pop(); // 2
stack.toArray(); // [1]API
Stack<T>
Constructor
new Stack<T>()- Creates an empty stack
Methods
isEmpty(): boolean- Check if the stack is emptypush(value: T): void- Add element to the toppop(): T | null- Remove and return the top elementpeek(): T | null- View the top element without removingtoArray(): T[]- Convert to array (top element first)
Contributing
Read the contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes.
License
This project is licensed under the terms of the MIT license.
