Collection Kit

Stack

Linear

A Stack is a linear data structure that follows the Last In First Out (LIFO) principle. Elements can be added and removed only from the top of the stack.

Import Statement
import { Stack } from "collection-kit";

Key Features

  • LIFO Order: The last element added is the first one to be removed.
  • Dynamic Size: Can grow and shrink as elements are added or removed.

Common Operations

Push
Add an element to the top of the stack.
Pop
Remove and return the top element.
Peek
Return the top element without removing it.
Size
Return the number of elements in the stack.

Example Code

import { Stack } from "collection-kit";

const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);

console.log("Top element:", stack.peek()); // 30
console.log("Popped:", stack.pop());       // 30
console.log("Size:", stack.size());        // 2