-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
39 lines (31 loc) · 661 Bytes
/
queue.js
File metadata and controls
39 lines (31 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
// Queue data structure a first in/first out structuretou
function Queue() {
this.next = null;
this.length = 0;
}
Queue.prototype.enqueue = function(value) {
this[this.length] = value;
if(!this.length) this.next = 0;
this.length++;
};
Queue.prototype.dequeue = function() {
if(this.length === 0) return;
--this.length;
let result = this[this.next];
delete this[this.next];
this.next++;
return result;
};
let nums = new Queue;
nums.enqueue(2);
nums.enqueue(3);
nums.enqueue(7);
nums.enqueue(10);
nums.enqueue(5);
nums.enqueue(4);
console.log(nums);
nums.dequeue();
nums.enqueue(18);
nums.dequeue();
console.log(nums);