forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (35 loc) · 852 Bytes
/
index.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
const Stack = require('../index');
class ImmitateQueue {
constructor() {
this.stackA = new Stack();
this.stackB = new Stack();
this.data = this.stackA.data;
}
add(element) {
this.stackA.push(element);
this.data = this.stackA.data;
}
peek() {
while (this.stackA.peek()) {
this.stackB.push(this.stackA.pop());
}
const element = this.stackB.peek();
while (this.stackB.peek()) {
this.stackA.push(this.stackB.pop());
}
this.data = this.stackA.data;
return element;
}
remove() {
while (this.stackA.peek()) {
this.stackB.push(this.stackA.pop());
}
const element = this.stackB.pop();
while (this.stackB.peek()) {
this.stackA.push(this.stackB.pop());
}
this.data = this.stackA.data;
return element;
}
}
module.exports = ImmitateQueue;