-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
36 lines (35 loc) · 933 Bytes
/
queue.js
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
const queuePrototype = {
enQueue: function(item) {
if (this.back - this.front < this.maxSize) {
this.storage[this.back] = item;
this.back++;
}
},
dequeue: function() {
if (this.front < this.back) {
const de = this.storage[this.front];
delete this.storage[this.front];
this.front++;
}
},
getQuantity: function getQuantity() {
return this.back - this.front;
},
isEmpty: function isEmpty() {
return this.back - this.front === 0;
},
isFull: function isFull() {
return this.back - this.front === this.maxSize;
},
peek: function peek() {
return this.storage[this.front];
}
}
function createQueue(maxSize=1) {
const queue = Object.create(queuePrototype)
queue.maxSize = maxSize
queue.front = 0
queue.back = 0
queue.storage = {}
return queue
}