Chapter4:普通队列(原理)

来源:互联网 发布:淘宝客服退款中心 编辑:程序博客网 时间:2024/06/06 00:55

package chapter4;

public class Queue {

 /**
  * @普通队列
  */
 public static void main(String[] args) {
  QueueX theQueue = new QueueX(5);
  theQueue.insert(10);
  theQueue.insert(20);
  theQueue.insert(30);
  theQueue.insert(40);
  
  theQueue.remove();
  theQueue.remove();
  theQueue.remove();
  
  theQueue.insert(50);
  theQueue.insert(60);
  theQueue.insert(70);
  theQueue.insert(80);
  
  while(!theQueue.isEmpty()){
   int temp = theQueue.remove();
   System.out.println(temp);
   
  }
 }

}

class QueueX{
 private int maxSize;
 private int[] queArray;
 private int front;
 private int rear;
 private int nItems;
 
 public QueueX(int n){
  maxSize = n;
  queArray = new int[maxSize];
  front = 0;
  rear = -1;
  nItems = 0;
 }
 public void insert(int value){
  if(rear == maxSize-1)
   rear =-1;
  queArray[++rear]=value;
  nItems++;
 }
 public int remove(){
  int temp = queArray[front++];
  if(front==maxSize)
   front=0;
  nItems--;
  return temp;
 }
 public int peekFront(){
  return queArray[front];
 }
 public boolean isEmpty(){
  return (nItems==0);
 }
 public boolean isFull(){
  return (nItems==maxSize);
 }
 public int size(){
  return nItems;
 }
}

0 0