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

      

      

  • 相关阅读:
    java ssh整合报错:java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
    Ubuntu16.04安装搜狗输入法后有黑边问题的解决方法
    socket
    vim编辑器的使用
    linux用户和群组
    bash shell
    [LightOJ 1128]Greatest Parent
    [Luogu P4180][BJWC 2010]严格次小生成树
    函数、方法区别
    有关_meta内容(持续更新)
  • 原文地址:https://www.cnblogs.com/BaiGuodong/p/5575225.html
Copyright © 2011-2022 走看看