queue data structure in javascript 2018

Solutions on MaxInterview for queue data structure in javascript 2018 by the best coders in the world

showing results for - "queue data structure in javascript 2018"
Alessia
10 Jan 2020
1var Queue = (()=>{
2  const map = new WeakMap();
3  let _items = []
4  class Queue{
5    constructor(...items){
6      //initialize the items in queue
7      map.set(this, []);
8      _items = map.get(this);
9      // enqueuing the items passed to the constructor
10      this.enqueue(...items)
11    }
12    
13    enqueue(...items){
14      //push items into the queue
15      this._items = [...this._items, ...items]
16      return this._items;
17    }
18    
19    dequeue(count=1){
20      //pull out the first item from the queue
21      _items.splice(0,count);
22      return this._items;
23    }
24    
25    peek(){
26      //peek at the first item from the queue
27      return _items[0]
28    }
29    
30    size(){
31      //get the length of queue
32      return _items.length
33    }
34    
35    isEmpty(){
36      //find whether the queue is empty or no
37      return _items.length===0
38    }
39    
40    toArray(){
41      return _items
42    }
43    
44  }
45  return Queue
46})()