-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay66.js
More file actions
18 lines (17 loc) · 770 Bytes
/
Day66.js
File metadata and controls
18 lines (17 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Q6: Implement enqueue and dequeue using only two stacks
// Enqueue means to add an element, dequeue to remove an element.
var inputStack = []; // First stack
var outputStack = []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue (stackInput, item) ‹ return stackInput. push (item) ;
}
function dequeue (stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
Il get the first element that was ever pushed into the input stack
if (stackOutput. length <= 0) {
while(stackInput. length > 0) <
var elementToOutput = stackInput. pop ();
stackOutput. push (elementToOutput) ;
return stackOutput.pop ();
}