Array as a Queue
JS
S
JavaScriptBasic Queue implementation in JavaScript
1function Queue(values){
2 if(this.values.constructor !== Array) {
3 this.values = [values];
4 }
5 else {
6 this.values = values;
7 }
8}
9
10Queue.prototype.next = function(){
11 return this.values.shift();
12};
13
14Queue.prototype.push = function(value){
15 return this.values.push(value);
16};
17
18const data = [1, 2, 3, 4, 5, 6];
19const q = new Queue(data);
20
21q.next();
22q.next();Created on 12/5/2017