Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/can-dy-jack/stack
stack implementation in JavaScript
https://github.com/can-dy-jack/stack
data-structures javascript stack
Last synced: 6 days ago
JSON representation
stack implementation in JavaScript
- Host: GitHub
- URL: https://github.com/can-dy-jack/stack
- Owner: can-dy-jack
- License: mit
- Created: 2023-01-17T13:01:06.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-18T02:21:40.000Z (almost 2 years ago)
- Last Synced: 2024-10-22T15:41:26.204Z (17 days ago)
- Topics: data-structures, javascript, stack
- Language: JavaScript
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stack
stack implementation in JavaScript> 后入先出 - LIFO (last-in-first-out)
> 栈用于解决进制转换、回文、DFS算法、模拟递归等问题## install
```sh
npm i @kartjim/stack
```## export
```js
const { Stack } = require('@kartjim/stack');
```or ESM :
```js
import { Stack } from "@kartjim/stack";
```## Stack API
```ts
export class Stack {
constructor(arr?: T[]);
isEmpty(): boolean;
size(): number;
peek(): T;
push(item: T): Stack;
pop(): T;
}
```
### constructor
create a Stack from an Array.```js
new Stack();
// or
// new Stack([1, 2, 3]);
```
### isEmpty
Checks if the stack is empty.
```js
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.
```js
console.log(stack.size()) // 2
```
### peek
return the top element in the stack.
```js
console.log(stack.peek()) // 3
stack.pop();
stack.pop();
console.log(stack.peek()) // undefined
```
### push
push an element to the top of the stack.
```js
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.
```js
console.log(stack.pop()) // 7
console.log(stack.size()) // 1
stack.pop();
console.log(stack.pop()) // undefined
```## Coverage
`🚀 grunt coverage` :```sh
============= Coverage summary =============
Statements : 100% ( 56/56 )
Branches : 100% ( 6/6 )
Functions : 100% ( 6/6 )
Lines : 100% ( 55/55 )
============================================
```