I implemented a queue with two stacks like so:
class QueueTwoStacks {
constructor() {
this.stack1 = []
this.stack2 = []
}
enqueue(item) {
this.stack1.push(item)
const lastItem = this.stack1.pop()
this.stack2.push(lastItem)
const lastItem2 = this.stack2.pop()
this.stack1.push(lastItem2)
}
dequeue() {
if(this.stack1.length === 0) throw Error
return this.stack1.shift();
}
}
The course I'm following gave this as an answer:
class QueueTwoStacks {
constructor() {
this.inStack = [];
this.outStack = [];
}
enqueue(item) {
this.inStack.push(item);
}
dequeue() {
if (this.outStack.length === 0) {
// Move items from inStack to outStack, reversing order
while (this.inStack.length > 0) {
const newestInStackItem = this.inStack.pop();
this.outStack.push(newestInStackItem);
}
// If outStack is still empty, raise an error
if (this.outStack.length === 0) {
throw new Error("Can't dequeue from empty queue!");
}
}
return this.outStack.pop();
}
}
Is one better than the other, and if so, why? I feel like my solution is better because you don't have to loop, but maybe you're not supposed to do all the operation in the enqueue method?