-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
83 lines (69 loc) · 1.93 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// IMPLEMENT A QUEUE ~~~~ FIFO
/* Breadth first search, pop up messages, events http requests */
var Queue = function(capacity) {
this._capacity = capacity || Infinity;
this._dataStorage = {};
this._head = 0;
this._tail = 0;
};
Queue.prototype.enqueue = function(value) {
if (this._count() < this._capacity) {
this._dataStorage[this._tail++] = value;
return this._count();
}
return "Max capacity is reached, please remove an item before adding a new one";
};
Queue.prototype.dequeue = function() {
var element = this._dataStorage[this._head];
delete this._dataStorage[this._head];
if (this._head < this._tail) {
this._head++;
}
return element;
};
Queue.prototype.peek = function() {
return this._dataStorage[this.head];
};
Queue.prototype.count = function() {
return this._tail - this._head;
};
Queue.prototype.contains = function(value) {
for (var i = this._head; i < this._tail; i++) {
if (this._dataStorage[i] === value) {
return true;
}
}
return false;
};
Queue.prototype.until = function(value) {
for (var i = this._head; i < this._tail; i++) {
if (this._dataStorage[i] === value) {
return i - this._head + 1;
}
}
return null;
};
// IMPLEMENT A QUEUE WITH 2 STACKS
function Queue_TwoStacks() {
this._stackIn = new Stack();
this._stackOut = new Stack();
}
Queue_TwoStacks.prototype.enqueue = function(value) {
this._stackIn.push(value);
};
Queue_TwoStacks.prototype.transferStacks = function() {
while (this._stackIn.count() > 0) {
this._stackOut.push(this._stackIn.pop());
}
};
Queue_TwoStacks.prototype.dequeue = function() {
if (this._stackOut.count() === 0) this._transferStacks;
return this._stackOut.pop();
};
Queue_TwoStacks.prototype.count = function() {
return this._stackIn.count() + this._stackOut.count();
};
Queue_TwoStacks.prototype.peek = function() {
if (this._stackOut.count() === 0) this._transferStacks();
return this._stackOut.peek();
};