注意优先队列在插入时已排序。故在删除,不是以原始插入数据为顺序的。
还要了解两种队列的优点缺少,适合应用的场合。。。
1 class Queue 2 { 3 private int maxSize; 4 private long[] queArray; 5 private int front; 6 private int rear; 7 private int nItems; 8 9 public Queue(int s) 10 { 11 maxSize = s; 12 queArray = new long[maxSize]; 13 front = 0; 14 rear = -1; 15 nItems = 0; 16 } 17 public void insert(long j) 18 { 19 if(rear == maxSize - 1) 20 rear = -1; 21 queArray[++rear] = j; 22 nItems++; 23 System.out.print("Insert value " + j + " into the queue,\t"); 24 System.out.print("nItems value is " + nItems); 25 System.out.print("\trear value is " + rear); 26 System.out.println("\tfront value is " + front); 27 } 28 public long remove() 29 { 30 long temp = queArray[front++]; 31 if(front == maxSize) 32 front = 0; 33 nItems--; 34 System.out.print("Remove value " + temp + " from the queue,\t"); 35 System.out.print("nItems value is " + nItems); 36 System.out.print("\trear value is " + rear); 37 System.out.println("\tfront value is " + front); 38 return temp; 39 } 40 public long peekFront() 41 { 42 return queArray[front]; 43 } 44 public boolean isEmpty() 45 { 46 return (nItems == 0); 47 } 48 public boolean isFull() 49 { 50 return (nItems == maxSize); 51 } 52 public int size() 53 { 54 return nItems; 55 } 56 } 57 class PriorityQ 58 { 59 private int maxSize; 60 private long[] queArray; 61 private int nItems; 62 63 public PriorityQ(int s) 64 { 65 maxSize = s; 66 queArray = new long[maxSize]; 67 nItems = 0; 68 } 69 public void insert(long item) 70 { 71 int j; 72 73 if(nItems == 0) 74 queArray[nItems++] = item; 75 else 76 { 77 for(j = nItems - 1; j >= 0; j--) 78 { 79 if(item > queArray[j]) 80 queArray[j + 1] = queArray[j]; 81 else 82 break; 83 } 84 queArray[j + 1] = item; 85 nItems++; 86 System.out.print("Insert item " + item + " into the priority queue,\t"); 87 System.out.println("nItems value is " + nItems); 88 } 89 } 90 public long remove() 91 { 92 long temp = queArray[--nItems]; 93 System.out.print("Remove item " + temp + " from the priority queue,\t"); 94 System.out.println("nItems value is " + nItems); 95 return temp; 96 } 97 public long peekMin() 98 { 99 return queArray[nItems - 1]; 100 } 101 public boolean isEmpty() 102 { 103 return (nItems == 0); 104 } 105 public boolean isFull() 106 { 107 return (nItems == maxSize); 108 } 109 } 110 public class QueueApp { 111 112 /** 113 * @param args 114 */ 115 public static void main(String[] args) { 116 // TODO Auto-generated method stub 117 System.out.println("Queue Operation:"); 118 System.out.println(""); 119 Queue myQueue = new Queue(20); 120 121 myQueue.insert(10); 122 myQueue.insert(20); 123 myQueue.insert(30); 124 myQueue.insert(40); 125 126 myQueue.remove(); 127 myQueue.remove(); 128 myQueue.remove(); 129 130 myQueue.insert(50); 131 myQueue.insert(60); 132 myQueue.insert(70); 133 myQueue.insert(80); 134 myQueue.insert(90); 135 myQueue.insert(99); 136 System.out.println(""); 137 System.out.println("Priority Queue Operation:"); 138 System.out.println(""); 139 PriorityQ myPQ = new PriorityQ(20); 140 myPQ.insert(200); 141 myPQ.insert(400); 142 myPQ.insert(100); 143 myPQ.insert(300); 144 myPQ.insert(500); 145 146 myPQ.remove(); 147 myPQ.remove(); 148 149 myPQ.insert(600); 150 myPQ.insert(700); 151 152 153 154 } 155 156 }
输出:
时间: 2025-01-24 14:19:14