1.0.1 • Published 2 years ago
@fyears/tsqueue v1.0.1
implementation of Queue and Stack in TypeScript/JavaScript
idea
Inspired from this.
An array is a stack in js. But Array.prototype.shift()
is NOT an O(1) operation. So a queue should be implemented using two stacks to get amortized O(1) pop
time.
install
npm i @fyears/tsqueue
usage
import { Queue } from '@fyears/tsqueue';
const q = new Queue<string>();
q.push('abc');
q.push('efg');
const xx = q.pop();
console.log(xx); // abc
console.log(q.length); // 1
q.pop();
console.log(q.length); // 0
const xx2 = q.pop();
console.log(xx2); // undefined