Skip to content

Latest commit

 

History

History
63 lines (48 loc) · 1.54 KB

File metadata and controls

63 lines (48 loc) · 1.54 KB

In addition to the above containers, Js-sdsl also provides three adapters: stack, queue and priority queue.

Stack

Last in first out structure.

const s = new Stack([1, 2, 3]);
s.push(4);                  // O(1)
s.pop();                    // O(1)
const t = s.top();          // O(1)
console.log(t);             // 3

Queue

First in first out structure.

const q = new Queue([1, 2, 3]);
q.push(4);                  // O(1)
q.pop();                    // O(1)
const f = q.front();        // O(1)
console.log(t);             // 1

PriorityQueue

Priority queue, heap implementation, a queue that guarantees that the largest element is always at the front of the queue, supports custom comparison functions.

const que = new PriorityQueue([1, 2, 3]);
que.push(4);                // O(logn)
que.pop();                  // O(logn)
const t = que.top();        // O(1)
console.log(t);             // 3

// custom comparison function
new PriorityQueue([1, 2, 3], (x, y) => x - y);

/* 
 * When the first parameter is an array, you can specify copy=false, 
 * then will directly change the original array instead of using copy
 */
new PriorityQueue([1, 2, 3], undefined, false);

Try it

<textarea id='input'> const que = new PriorityQueue([1, 2, 3]); que.push(4); // O(logn) que.pop(); // O(logn) const t = que.top(); // O(1) console.log(t); // 3 </textarea>

Run it Reset