1.0.0 • Published 3 years ago
@kartjim/stack v1.0.0
stack
stack implementation in JavaScript
后入先出 - LIFO (last-in-first-out)
栈用于解决进制转换、回文、DFS算法、模拟递归等问题
install
npm i @kartjim/stackexport
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()) // falsesize
return the number of elements in the stack.
console.log(stack.size()) // 2peek
return the top element in the stack.
console.log(stack.peek()) // 3
stack.pop();
stack.pop();
console.log(stack.peek()) // undefinedpush
push an element to the top of the stack.
stack.push(5);
stack.push(7)
console.log(stack.peek()) // 7
console.log(stack.size()) // 2pop
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()) // undefinedCoverage
🚀 grunt coverage :
============= Coverage summary =============
Statements : 100% ( 56/56 )
Branches : 100% ( 6/6 )
Functions : 100% ( 6/6 )
Lines : 100% ( 55/55 )
============================================1.0.0
3 years ago