Skip to main content
Home

latest

A class stack implementation in Typescript for any generic type 'T'

This package works with Node.js, Deno, BunIt is unknown whether this package works with Cloudflare Workers, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
a year ago (1.0.3)

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:

  1. 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
    
  2. Import the Stack class from this module into your TypeScript files:

    import { Stack } from '@cm-iv/stack';
    
  3. Create a new instance of the Stack class:

    const stack = new Stack<number>(); // Create a stack of numbers
    
  4. 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

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@cm-iv/stack

Import symbol

import * as stack from "@cm-iv/stack";
or

Import directly with a jsr specifier

import * as stack from "jsr:@cm-iv/stack";

Add Package

pnpm i jsr:@cm-iv/stack
or (using pnpm 10.8 or older)
pnpm dlx jsr add @cm-iv/stack

Import symbol

import * as stack from "@cm-iv/stack";

Add Package

yarn add jsr:@cm-iv/stack
or (using Yarn 4.8 or older)
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";