function Queue() {
this.dataStore = [];
this.enqueue = function(item) {
this.dataStore.push(item);
return this.dataStore;
}
this.dequeue = function() {
return this.dataStore.shift();
}
this.length = function() {
return this.dataStore.length;
}
}
function Stack() {
var queue1 = new Queue();
var queue2 = new Queue();
this.push = function(item){
let curQueue = queue1;
curQueue = !!queue2.length() ? queue2 : queue1;
return curQueue.enqueue(item);
}
this.pop = function(){
if(!queue1.length() && !queue2.length()) {
throw new Error('Stack is Empty');
}
const curQueue = !!queue1.length() ? queue1 : queue2;
const tmpQueue = curQueue === queue1 ? queue2 : queue1;
while(curQueue.length() > 1) {
tmpQueue.enqueue(curQueue.dequeue());
}
return curQueue.dequeue();
}
}
var stack = new Stack();
console.log(stack.push(2));
console.log(stack.push(4));
console.log(stack.push(6));
console.log(stack.pop());
console.log(stack.push(8));
console.log(stack.push(10));
console.log(stack.pop());
console.log(stack.pop());
console.log(stack.pop());
console.log(stack.pop());