TypeScript Stack Data Structure
This repository contains a simple implementation of a stack data structure in TypeScript. The stack follows the Last In, First Out (LIFO) principle, where elements are added and removed from the top of the stack.
Table of Contents
Stack Overview
A stack is a linear data structure that follows the principle of Last In, First Out (LIFO). It means that the element added last will be removed first. The operations of adding and removing elements occur at the same end, known as the top of the stack.
Usage
To use this stack data structure in your TypeScript projects, you can follow these steps:
-
Add this library to your local project with jsr:
# Deno deno add @cm-iv/stack # npm npx jsr add @cm-iv/stack # pnpm pnpm dlx jsr add @cm-iv/stack # Bun bunx jsr add @cm-iv/stack
-
Import the Stack class from this module into your TypeScript files:
import { Stack } from '@cm-iv/stack';
-
Create a new instance of the Stack class:
const stack = new Stack<number>(); // Create a stack of numbers
-
Use the stack methods to manipulate and retrieve data:
stack.push(5); // Push an element onto the stack stack.pop(); // Pop an element from the stack stack.peek(); // Peek at the top element without removing it stack.isEmpty(); // Check if the stack is empty stack.size(); // Get the size of the stack stack.clear(); // Clear the stack stack.print(); // Print the stack elements
Methods
push(element: T): // Adds an element to the top of the stack. pop(): T | undefined: // Removes and returns the top element from the stack. peek(): T | undefined: // Returns the top element of the stack without removing it. isEmpty(): boolean: // Checks if the stack is empty. size(): number: // Returns the size of the stack. clear(): void: // Removes all elements from the stack. print(): void: // Prints the elements of the stack.
License
This project is licensed under the MIT License
Add Package
deno add jsr:@cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";
Import directly with a jsr specifier
import * as stack from "jsr:@cm-iv/stack";
Add Package
pnpm i jsr:@cm-iv/stack
pnpm dlx jsr add @cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";
Add Package
yarn add jsr:@cm-iv/stack
yarn dlx jsr add @cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";
Add Package
vlt install jsr:@cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";
Add Package
npx jsr add @cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";
Add Package
bunx jsr add @cm-iv/stack
Import symbol
import * as stack from "@cm-iv/stack";