-
Notifications
You must be signed in to change notification settings - Fork 3
/
232.js
34 lines (30 loc) · 808 Bytes
/
232.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
function Queue() {
var stackPush = [],
stackPop = []
this.push = function(newNum) {
stackPush.push(newNum)
}
this.pop = function() {
if(stackPop.length === 0 && stackPush.length === 0) {
throw new Error('Queue is empty!')
} else if(stackPop.length === 0) {
while(stackPush.length !== 0) {
stackPop.push(stackPush.pop())
}
}
return stackPop.pop()
}
this.peek = function() {
if(stackPop.length === 0 && stackPush.length === 0) {
throw new Error('Queue is empty!')
} else if(stackPop.length === 0) {
while(stackPush.length !== 0) {
stackPop.push(stackPush.pop())
}
}
return stackPop[stackPop.length - 1]
}
this.empty = function() {
return stackPop.length === 0 && stackPush.length === 0
}
}