-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13. Implement a Queue by using Stack.js
49 lines (43 loc) · 1.28 KB
/
13. Implement a Queue by using Stack.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
/* you can use this Class which is bundled together with your code
class Stack {
push(element) { // add element to stack }
peek() { // get the top element }
pop() { // remove the top element}
size() { // count of element }
}
*/
/* Array is disabled in your code */
// you need to complete the following Class
class Queue {
constructor() {
this.stack = new Stack();
}
enqueue(element) {
return this.stack.push(element);
}
peek() {
// reverse the stack and peek on the reversed stack
return this.reverseStack(this.stack).peek();
}
size() {
// return count of element
return this.stack.size();
}
dequeue() {
// overwrite the original stack with the reversed stack
this.stack = this.reverseStack(this.stack);
// remove the head element (tail since we reversed it)
return this.stack.pop();
}
// Private function to hide the internal details of queue
reverseStack(stack) {
// the idea is to use another stack temporarily to tranfer items in reverse order
const tempStack = new Stack();
// remove all items from the stack and store it in tempStack
while (stack.size() > 0) {
tempStack.push(stack.pop());
}
// return the tempStack
return tempStack;
}
}