Chapter4:优先级队列(原理)

本文详细介绍了优先级队列的基本概念,并通过代码实例展示了如何实现和使用它。包括插入元素、移除最小元素、查找最小元素以及检查队列是否为空或已满的功能。

package chapter4;

public class PriorityQ {

 /**
  * @优先级队列
  */
 public static void main(String[] args) {
  PriorityX thePri = new PriorityX(5);
  thePri.insert(30);
  thePri.insert(50);
  thePri.insert(10);
  thePri.insert(40);
  thePri.insert(20);
  
  while(!thePri.isEmpty()){
   int temp = thePri.remove();
   System.out.println(temp);
  }
 }

}

class PriorityX{
 private int maxSize;
 private int[] queArray;
 private int nItems;
 
 public PriorityX(int max){
  maxSize = max;
  queArray = new int[maxSize];
  nItems = 0;
 }
 public void insert(int value){
  int j;
  if(nItems==0)
   queArray[0] = value;
  else{
   for(j=nItems-1;j>=0;j--){
    if(queArray[j]<value){
     queArray[j+1] = queArray[j];
    }else
     break;
   }
   queArray[j+1] = value;
  }
  nItems++;
 }
 public int remove(){
  return queArray[--nItems];
 }
 public int peekMin(){
  return queArray[nItems-1];
 }
 public boolean isEmpty(){
  return (nItems==0);
 }
 public boolean isFull(){
  return (nItems==maxSize);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值