js 模拟队列类

/*
* 模拟队列
*/
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);

转载于:https://www.cnblogs.com/lsmsky/archive/2011/12/07/2279573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值