/*
* 模擬隊列
*/
var Qu ={};
//構造函數
Qu.Queue = function (len) {
this.capacity = len; //隊列最大容量
this.list = new Array(); //隊列數據
};
//入隊
Qu.Queue.prototype.enqueue = function (data) {
if (data == null) return;
if(this.list.length>=this.capacity)
{
this.list.remove(0);
}
this.list.push(data);
};
//出隊
Qu.Queue.prototype.dequeue = function () {
if (this.list == null) return;
this.list.remove(0);
};
//隊列長度
Qu.Queue.prototype.size = function () {
if (this == null) return;
return this.list.length;
};
//隊列是否空
Qu.Queue.prototype.isEmpty = function () {
if (this == null|this.list==null) return false;
return this.list.length>0;
};
?
//對象數組擴展remove
Array.prototype.remove = function(dx) {
if (isNaN(dx) || dx > this.length) {
return false;
}
for (var i = 0, n = 0; i < this.length; i++) {
if (this[i] != this[dx]) {
this[n++] = this[i]
}
}
this.length -= 1
}
調用例子:
//隊列初始化
var queue = new Qu.Queue(10);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
摘自 尋夢E.net