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

  • 相关阅读:
    排序——插入排序
    利用socket传文件
    Segmentation fault (core dumped)
    Linux网络编程
    3G功能设计及实现
    rpm命令
    安装包相互依赖的问题
    centos网站(一些软件的下载)
    解决vim显示中文的问题
    glGetFloatv (GL_MODELVIEW_MATRIX, mat)
  • 原文地址:https://www.cnblogs.com/lsmsky/p/2279573.html
Copyright © 2020-2023  润新知