数组模拟队列

public class ArralQuene {
public int arr[];
private int front;
private int rear;

public ArralQuene(int size) {
    if (size <= 0) {
        throw new RuntimeException("长度不能为空");
    }
    arr = new int[size];
}

public boolean isEmpty() {

    return front == rear;

}

public boolean isFull() {
    return rear == arr.length;
}

public void add(int a) {
    if (isFull()) {
        System.out.println("满了");
        return;
    }
    arr[rear] = a;
    rear++;
}

public int get() {
    if (isEmpty()) {
        System.out.println("没了");
        throw new RuntimeException("异常");
    }
    return arr[front++];
  
}

public int length() {
    return rear - front;
}

}

你可能感兴趣的:(数组模拟队列)