java数据结构循环队列
时间: 2023-11-06 12:08:09 浏览: 133
循环队列是一种特殊的队列,它可以在固定大小的数组中实现队列的基本操作。循环队列的特点是队尾指针可以回到数组的开头,形成一个环形结构,从而实现循环利用数组空间。下面是 Java 实现循环队列的代码:
```java
public class CircularQueue {
private int[] queue;
private int front;
private int rear;
private int size;
public CircularQueue(int k) {
queue = new int[k];
front = 0;
rear = -1;
size = 0;
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
rear = (rear + 1) % queue.length;
queue[rear] = value;
size++;
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
front = (front + 1) % queue.length;
size--;
return true;
}
public int Front() {
if (isEmpty()) {
return -1;
}
return queue[front];
}
public int Rear() {
if (isEmpty()) {
return -1;
}
return queue[rear];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == queue.length;
}
}
```
阅读全文