zoukankan      html  css  js  c++  java
  • JavaScript数据结构-6.优先队列

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6     </head>
     7     <body>
     8         
     9         <script>
    10             
    11             //优先队列的实现
    12             
    13             function Person(name,code){
    14                 this.name = name;
    15                 this.code = code;
    16             }
    17             
    18             function Queue(){
    19                 this.arr = [];
    20                 this.enqueue = enqueue;
    21                 this.dequeue = dequeue;
    22                 this.front = front;
    23                 this.back = back;
    24                 this.toString = toString;
    25                 this.empty = empty;
    26             }
    27             function enqueue(ele){
    28                 this.arr.push(ele);
    29             }
    30             function dequeue(){
    31                 var code = this.arr[0].code,value,index; 
    32                 for(var i=0;i<this.arr.length;i++){
    33                     if(code >this.arr[i].code){
    34                         index = i;    //数字越小优先级越高
    35                         code = this.arr[i].code;
    36                     }
    37                 }
    38                 value = this.arr[index];
    39                 this.arr.splice(index,1);
    40                 return value;
    41             }
    42             
    43             function front(){
    44                 return this.arr[0];
    45             }
    46             function back(){
    47                 return this.arr[this.arr.length-1];
    48             }
    49             function toString(){
    50                 var retstr = '';
    51                 for(var i=0;i<this.arr.length;i++){
    52                     retstr += this.arr[i].name +" "+this.arr[i].code+",";
    53                 }
    54                 return retstr;
    55             }
    56             
    57             function empty(){
    58                 return     this.arr.length == 0  
    59             }
    60             
    61             
    62             //调用
    63             var p = new Person("zhangsan","3")
    64             var q = new Queue();
    65             q.enqueue(p);
    66             p = new Person("lisi","2");
    67             q.enqueue(p);
    68             p = new Person("zhaowu","1");
    69             q.enqueue(p);
    70             p = new Person("lisi","4");
    71             q.enqueue(p);
    72             
    73             console.log(q.toString());
    74             q.dequeue();
    75             console.log(q.toString());
    76             
    77             q.dequeue();
    78             console.log(q.toString());
    79             q.dequeue();
    80             console.log(q.toString());
    81         </script>
    82     </body>
    83 </html>
  • 相关阅读:
    php省市联动实现
    json学习笔记
    ajax无刷新技术
    session和cookie
    缩略图,透明度,水印,图片合并
    PHP画矩形,椭圆,圆,画椭圆弧 ,饼状图
    PHP字母数字验证码和中文验证码
    PHP画图的基本步骤GD库
    文件上传和多文件上传
    js身份证验证
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7191882.html
Copyright © 2011-2022 走看看