232. 用栈实现队列
var MyQueue = function() {
this.queue = [];
};
MyQueue.prototype.push = function(x) {
this.queue.push(x);
};
MyQueue.prototype.pop = function() {
let res = this.queue[0];
this.queue.splice(0,1)
return res;
};
MyQueue.prototype.peek = function() {
return this.queue[0];
};
MyQueue.prototype.empty = function() {
if (this.queue.length === 0) {
return true;
}
for (let i = 0; i < this.queue; i++) {
if (this.queue[i] == '') {
return true;
}
}
return false;
};
225. 用队列实现栈
var MyStack = function() {
this.stack = [];
};
MyStack.prototype.push = function(x) {
this.stack.push(x);
};
MyStack.prototype.pop = function() {
return this.stack.pop();
};
MyStack.prototype.top = function() {
return this.stack[this.stack.length - 1];
};
MyStack.prototype.empty = function() {
if (this.stack.length == 0) {
return true;
}
for (let i = 0; i < this.stack.length; i++) {
if (this.stack == '') {
return true;
}
}
return false;
};