首页 > 其他 > 详细

队列的实现

时间:2016-04-09 23:12:34      阅读:264      评论:0      收藏:0      [点我收藏+]
    function Queue() {
        this.dataStore = [];
        this.enqueue = enqueue;
        this.dequeue = dequeue;
        this.front = front;
        this.back = back;
        this.toString = toString;
        this.empty = empty;
    }
    function enqueue(element) {//向队尾添加一个元素
        this.dataStore.push(element);
    }
    function dequeue() {//删除队首的元素
        return this.dataStore.shift();
    }
    function front() {//读取队首元素
        return this.dataStore[0];
    }
    function back() {//读取队尾元素
        return this.dataStore[this.dataStore.length - 1];
    }
    function toString() {
        var retStr = "";
        for ( var i = 0; i < this.dataStore.length; ++i) {
            retStr += this.dataStore[i] + " ";
        }
        return retStr;
    }
    function empty() {//判断队列是否为空
        if (this.dataStore.length == 0) {
            return true;
        } else {
            return false;
        }
    }
    var q = new Queue();
    q.enqueue("Meredith");
    q.enqueue("Cynthia");
    q.enqueue("Jennifer");
    document.write(q.toString() + "<br />");
    q.dequeue();
    document.write(q.toString() + "<br />");
    document.write("Front of queue: " + q.front() + "<br />");
    document.write("Back of queue: " + q.back() + "<br />");
    /*测试结果:
    Meredith Cynthia Jennifer 
    Cynthia Jennifer 
    Front of queue: Cynthia
    Back of queue: Jennifer  */

 

队列的实现

原文:http://www.cnblogs.com/feile/p/5372872.html

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