zoukankan      html  css  js  c++  java
  • 五、优先级队列

    1、优先级队列(数组实现)

    优先级队列是比栈和队列更专用的数据结构,优先级队列中数据项的值有序,优先级队列有一个队头和一个队尾,并且也是从队列头移除数据项。不过在优先级队列中,数据项按关键字的值有序,这样关键字最小数据项(或者在某些实现中是关键字最大的数据项)总在队头。数据项插入的时候会按照顺序插入到合适的位置以确保队列的顺序。优先级队列数组实现:数组头为队列尾,数组尾为队列头,队列中数据项的值从队列头至队列尾越来越大。

    插入:N/2   删除:O(1)

    // to run this program: C>java PriorityQApp
    class PriorityQ
       {
       // array in sorted order, from max at 0 to min at size-1
       private int maxSize;
       private long[] queArray;
       private int nItems;
    //-------------------------------------------------------------
       public PriorityQ(int s)          // constructor
          {
          maxSize = s;
          queArray = new long[maxSize];
          nItems = 0;
          }
    //-------------------------------------------------------------
       public void insert(long item)    // insert item
          {
          int j;
          if(nItems==0)                         // if no items,
             queArray[nItems++] = item;         // insert at 0
          else                                // if items,
             {
             for(j=nItems-1; j>=0; j--)         // start at end,
                {
                if( item > queArray[j] )      // if new item larger,
                   queArray[j+1] = queArray[j]; // shift upward
                else                          // if smaller,
                   break;                     // done shifting
                }  // end for
             queArray[j+1] = item;            // insert it
             nItems++;
             }  // end else (nItems > 0)
          }  // end insert()
    //-------------------------------------------------------------
       public long remove()             // remove minimum item
          { return queArray[--nItems]; }
    //-------------------------------------------------------------
       public long peekMin()            // peek at minimum item
          { return queArray[nItems-1]; }
    //-------------------------------------------------------------
       public boolean isEmpty()         // true if queue is empty
          { return (nItems==0); }
    //-------------------------------------------------------------
       public boolean isFull()          // true if queue is full
          { return (nItems == maxSize); }
    //-------------------------------------------------------------
       }  // end class PriorityQ
    ////////////////////////////////////////////////////////////////
    class PriorityQApp
       {
       public static void main(String[] args)
          {
          PriorityQ thePQ = new PriorityQ(5);
          thePQ.insert(30);
          thePQ.insert(50);
          thePQ.insert(10);
          thePQ.insert(40);
          thePQ.insert(20);
    
          while( !thePQ.isEmpty() )
             {
             long item = thePQ.remove();
             System.out.print(item + " ");  // 10, 20, 30, 40, 50
             }  // end while
          System.out.println("");
          }  // end main()
    //-------------------------------------------------------------
       }  // end class PriorityQApp
    ////////////////////////////////////////////////////////////////
  • 相关阅读:
    博客内容分类查重分析平台——大致任务划分(根据进度更改)
    大数据学习——MapReduce配置及java代码实现wordcount算法
    大数据学习——java代码实现对HDFS文件的read、append、write操作
    大数据学习——CentOS6.5在虚拟机下搭建HDFS
    大数据学习——CentOS6.5在虚拟机下实现ssh免秘钥登录
    大数据学习——CentOS6.5在虚拟机下安装mysql
    大数据学习——CentOS6.5在虚拟机下配置jdk
    大数据学习——CentOS6.5在虚拟机下安装配置ip网络
    MVC架构之二
    MVC架构
  • 原文地址:https://www.cnblogs.com/xxlong/p/4980335.html
Copyright © 2011-2022 走看看