zoukankan      html  css  js  c++  java
  • Javascript中的队列

    队列遵循FIFO (First In First Out)原则。

    普通队列

          function Queue() {
            var items=[];
            //向队列尾部添加一个或者多个元素
            this.enqueue=function(element) {
              items.push(element);
            }
            // 移除队列最后一个
            this.dequeue=function () {
              return items.shift();
            }
            // 返回队列第一个
            this.front=function () {
              return items[0];
            }
            // 队列是否为空
            this.isEmpty=function(){
              return items.length==0;
            }
            // 返回队列长度
            this.size=function () {
              return items.length;
            }
            // 打印
            this.print=function(){
              console.log(items.toString());
            }
          }
    

      优先级队列

    function PriorityQueue() {
            var items=[];
            function QueueElement(element,priority) {
              this.element=element;
              this.priority=priority;
            }
            // priority 数值越大,优先级越低
            this.enqueue=function (element,priority) {
              var queueElement=new QueueElement(element,priority);
              if(this.isEmpty()){
                items.push(queueElement);
              }else{
                var added=false;
                for (var i = 0; i < items.length; i++) {
                  if(queueElement.priority<items[i].priority){
                    items.splice(i,0,queueElement);
                    added=true;
                    break;
                  }
                }
                if(!added){
                  items.push(queueElement);
                }
              }
            }
            // 移除队列最后一个
            this.dequeue=function () {
              return items.shift();
            }
            // 返回队列第一个
            this.front=function () {
              return items[0];
            }
            // 队列是否为空
            this.isEmpty=function(){
              return items.length==0;
            }
            // 返回队列长度
            this.size=function () {
              return items.length;
            }
            // 打印
            this.print=function(){
              console.log(JSON.stringify(items));
            }
    }
    

    通过击鼓传花演示循环队列

    function hotPotato(nameList,num){
            var queue=new Queue(),eliminated="";
            for (var i = 0; i < nameList.length; i++) {
              queue.enqueue(nameList[i]);
            }
            while (queue.size()>1) {
              for (var i = 0; i < num; i++) {
                queue.enqueue(queue.dequeue());
              }
              eliminated=queue.dequeue();
              console.log(eliminated+"被淘汰");
            }
            return queue.dequeue();
     }
    

      

      

  • 相关阅读:
    网络编程学习小结
    我的学习笔记_Windows_HOOK编程 2009-12-03 11:19
    void及void指针含义的深刻解析
    Android开发之自己定义TabHost文字及背景(源码分享)
    ActionBar自己定义改动无效解决方法
    一位Erlang程序猿的自白
    Xcode 5.1安装插件:规范凝视生成器VVDocumenter
    Socket程序中的Error#10054错误
    CSDN博客清理缓存
    ACM 位运算
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/5575225.html
Copyright © 2011-2022 走看看