添加232.用栈实现队列JavaScript版本

This commit is contained in:
qingyi.liu
2021-05-26 16:25:47 +08:00
parent b2c04ffbca
commit 4eb950d5f7

View File

@ -353,6 +353,61 @@ func (this *MyQueue) Empty() bool {
*/
```
javaScript:
```js
// 使用两个数组的栈方法push, pop 实现队列
/**
* Initialize your data structure here.
*/
var MyQueue = function() {
this.stack1 = [];
this.stack2 = [];
};
/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.stack1.push(x);
};
/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function() {
const size = this.stack2.length;
if(size) {
return this.stack2.pop();
}
while(this.stack1.length) {
this.stack2.push(this.stack1.pop());
}
return this.stack2.pop();
};
/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function() {
const x = this.pop();
this.stack2.push(x);
return x;
};
/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return !this.stack1.length && !this.stack2.length
};
```