package com.queue;
public class LoopQueue {
private int size; //队列有效长度
private int rear; //队尾,可以插入下一数据的位置
private int head; //对头,可以获取数据的第一个位置
private int[] arr;
public LoopQueue() {
this(10);
}
public LoopQueue(int arrLength) {
size = 0;
rear = 0;
head = 0;
arr = new int[arrLength];
}
public void push(int num) throws Exception {
if(isFull()) {//队列已满
throw new Exception("队列已满");
}
if(++rear == arr.length) {
rear = 0;
}
size++;
arr[rear] = num;
}
public int pop() throws Exception {
if(isEmpty()) {//队列为空
throw new Exception("队列为空");
}
if(++head == arr.length) {
head = 0;
}
size--;
return arr[head];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == arr.length;
}
public int currentSize() {
return size;
}
}