用 js 实现了一个循环队列的, 代码如下. 可是老是通不过测试, 我真的不知道自己错在哪里. 衰啊
/**
* Initialize your data structure here. Set the size of the queue to be k.
* @param {number} k
*/
var MyCircularQueue = function (k) {
if (k == undefined) {
k = 0;
}
this.k = k;
this.p_head = 0;
this.p_tail = 0;
this._data = new Array(k);
};
/**
* Insert an element into the circular queue. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function (value) {
if (this.isFull() == true) {
return false
}
this.p_tail = this.p_tail % this.k;
this._data[this.p_tail++] = value;
return true;
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function () {
if (this.isEmpty() == true) {
return false
}
this.p_head++;
this.p_head = this.p_head % this.k;
return true;
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function () {
if (this.isEmpty() == true) {
return false
}
return this._data[this.p_head];
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function () {
if (this.isEmpty() == true) {
return false
}
// console.log(this._data[this.p_tail-1]);
return this._data[this.p_tail];
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function () {
return this.p_head == this.p_tail;
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function () {
return (this.p_tail + 1) % this.k == this.p_head;
};
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.