首页 > Web开发 > 详细

js队列的实现问题

时间:2014-03-12 16:24:48      阅读:468      评论:0      收藏:0      [点我收藏+]

所谓队列就是排队的序列问题,有出有进,比如在银行排队办理业务,一般都是前一个办理完成后下一个自动进入队列

<script>  /*

 * 模拟队列

 */

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); </script>

/*

 * 模拟队列

 */

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);

js队列的实现问题,布布扣,bubuko.com

js队列的实现问题

原文:http://www.cnblogs.com/dearxinli/p/3595651.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!