public class ArrayQueue { //数组最大容量 private int maxSize; //队列头,队列第一个不为空的数据的,前一个下标 private int front; //队列尾部,队列最后一个不为空的数据 private int rear; //队列数据容器 private int[] arr; // public ArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; front = -1; rear = -1; } //判断队列是否满 public boolean isFull() { return rear == maxSize - 1; } // 判断队列是否为空 public boolean isEmpty() { return front == rear; } // 添加数据到队列 public void addQueue(int n) { if (isFull()) { throw new RuntimeException("队列已满"); } rear++; arr[rear] = n; } // 获取队列的数据,出队列 public int getQueue() { if (isEmpty()) { throw new RuntimeException("队列数据不存在!"); } front++; return arr[front]; } // 显示队列的全部数据 public void showQueue() { if (!isEmpty()) { int num = rear - front; for (int i = 0; i < num; i++) { System.out.printf("%d\t%d\n",++front,arr[front]); } } } // 显示队列的头数据 public int headQueue(){ if (!isEmpty()) { return arr[front+1]; } throw new RuntimeException("数据为空"); } }