優先隊列(儲存結構數組)--Java實現

來源:互聯網
上載者:User

標籤:java   儲存結構   隊列的實現   效率   span   實現   div   有用   作業系統   

 1 /*優先隊列--是對隊列的一種改進 2  *要儲存的資料存在優先順序--數值小的優先順序高--在隊頭 3  *優先隊列的實現 4  *1.數組:適合資料量小的情況(沒有用rear+front實現) 5  *優先隊列頭在items-1,隊列尾在0是固定的 6  *2.堆:適合資料量大的情況 7  *優先隊列的效率:插入O(N)移除O(1) 8  *優先隊列的應用:作業系統線程調度演算法 9  * */10 public class MyPriorityQueue {11     private int maxSize;12     private long[] arr;//插入的時候保證有序13     private int items;14     15     public MyPriorityQueue(int s) {16         maxSize = s;17         arr = new long[maxSize];18         items = 0;19     }20     21     public void insert(long key){22         int j;23         if(items == 0){//為空白直接加入24             arr[items++] = key;25         }26         else{//不為空白就將小元素方在最上面--隊列頭27             for(j = items-1;j >= 0;j--){28                 if(key > arr[j]){29                     arr[j+1] = arr[j];30                 }31                 else{32                     break;33                 }34             }35             arr[j+1] = key;36             items++;37         }38     }39     40     public long remove(){41         return arr[--items];42     }43     44     public boolean isEmpty(){45         return items == 0;46     }47     48     public boolean isFull(){49         return items == maxSize;50     }51     52     public long getPeekMin(){53         return arr[items -1];54     }55     56     public void diaplayPQ(){57         for(int i = items- 1;i >= 0;i--){58             System.out.print(arr[i] + " ");59         }60         System.out.println();61     }62 }

 

優先隊列(儲存結構數組)--Java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.