html 模拟队列,js模拟队列类 - 南京HTML5培训_上海HTML5培训_合肥HTML5培训_web前端培训-欣才IT学院...

本文详细介绍了如何使用JavaScript实现一个队列数据结构,包括构造函数、入队、出队、队列长度判断和空队列检查方法。实例展示了如何初始化队列并操作元素,适合初学者理解基本数据结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

* 模拟队列

*/

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值