Chapter4:优先级队列(原理)

来源:互联网 发布:sql文件怎么导入数据库 编辑:程序博客网 时间:2024/05/16 15:00

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);
 }
}

0 0
原创粉丝点击