forked from OperationCode/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingStackR.js
More file actions
54 lines (43 loc) · 1.02 KB
/
QueueUsingStackR.js
File metadata and controls
54 lines (43 loc) · 1.02 KB
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
import Stack from '../stack/Stack'
export default class QueueR {
constructor() {
this.store = new Stack();
this.otherStack = new Stack();
}
isEmpty() {
return this.store.isEmpty();
}
peek() {
while (!this.store.isEmpty()){
this.otherStack.push(this.store.pop());
}
var ans = this.otherStack.peek();
while (!this.otherStack.isEmpty()){
this.store.push(this.otherStack.pop());
}
return ans;
}
enqueue(el) {
return this.store.push(el);
}
dequeue() {
while (!this.store.isEmpty()){
this.otherStack.push(this.store.pop());
}
var ans = this.otherStack.pop();
while (!this.otherStack.isEmpty()){
this.store.push(this.otherStack.pop());
}
return ans;
}
toString() {
while (!this.store.isEmpty()){
this.otherStack.push(this.store.pop());
}
var ans = this.otherStack.toString();
while (!this.otherStack.isEmpty()){
this.store.push(this.otherStack.pop());
}
return ans;
}
}