用java实现一个循环队列
时间: 2023-05-11 14:07:46 浏览: 121
好的,以下是用 Java 实现循环队列的代码:
public class CircularQueue {
private int[] data;
private int head;
private int tail;
private int size;
public CircularQueue(int k) {
data = new int[k];
head = -1;
tail = -1;
size = k;
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
if (isEmpty()) {
head = 0;
}
tail = (tail + 1) % size;
data[tail] = value;
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
return true;
}
head = (head + 1) % size;
return true;
}
public int Front() {
if (isEmpty()) {
return -1;
}
return data[head];
}
public int Rear() {
if (isEmpty()) {
return -1;
}
return data[tail];
}
public boolean isEmpty() {
return head == -1;
}
public boolean isFull() {
return ((tail + 1) % size) == head;
}
}
希望这个代码能够帮助到你!
阅读全文