@kartjim/stack
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

stack

stack implementation in JavaScript

后入先出 - LIFO (last-in-first-out)
栈用于解决进制转换、回文、DFS算法、模拟递归等问题

install

npm i @kartjim/stack

export

const { Stack } = require('@kartjim/stack');

or ESM :

import { Stack } from "@kartjim/stack";

Stack API

export class Stack<T> {
    constructor(arr?: T[]);
    isEmpty(): boolean;
    size(): number;
    peek(): T;
    push(item: T): Stack<T>;
    pop(): T;
}

constructor

create a Stack from an Array.

new Stack();
// or
// new Stack([1, 2, 3]);

isEmpty

Checks if the stack is empty.

console.log(stack.isEmpty()) // true
stack.push(5);
stack.push(3);
console.log(stack.isEmpty()) // false

size

return the number of elements in the stack.

console.log(stack.size()) // 2

peek

return the top element in the stack.

console.log(stack.peek()) // 3
stack.pop();
stack.pop();
console.log(stack.peek()) // undefined

push

push an element to the top of the stack.

stack.push(5);
stack.push(7)
console.log(stack.peek()) // 7
console.log(stack.size()) // 2

pop

remove and return the top element in the stack.

console.log(stack.pop()) // 7
console.log(stack.size()) // 1
stack.pop();
console.log(stack.pop()) // undefined

Coverage

🚀 grunt coverage :

============= Coverage summary =============
Statements   : 100% ( 56/56 )
Branches     : 100% ( 6/6 )
Functions    : 100% ( 6/6 )
Lines        : 100% ( 55/55 )
============================================

Dependents (1)

Package Sidebar

Install

npm i @kartjim/stack

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

5.16 kB

Total Files

5

Last publish

Collaborators

  • kartjim