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
    ////////////////////////////////////////////////////////////////
  • 相关阅读:
    洛谷1280 尼克的任务
    洛谷1140 相似基因
    洛谷1133 教主的花园
    洛谷1130 红牌
    洛谷1122 最大子树和
    洛谷1103 书本整理
    洛谷1077 摆花
    【数学】数学知识习题小结(模板)
    python中的深拷贝和浅拷贝(面试题二)
    python中的深拷贝和浅拷贝(面试题)
  • 原文地址:https://www.cnblogs.com/xxlong/p/4980335.html
Copyright © 2011-2022 走看看