CircularQueue.java
package com.example.demo.circularqueue;
/**
* @author sunweidong
*/
public class CircularQueue<T> {
/**
* 默认环形队列的容量,默认3600个元素
*/
public static final int DEFAULT_CAPACITY = 3600;
/**
* 队列头部
*/
private Integer head;
/**
* 队列尾部
*/
private Integer tail;
/**
* 队列容量
*/
private Integer capacity;
/**
* 队列元素数组
*/
private T[] elements;
/**
* 构造一个给定容量的环形队列
*
* @param capacity 环形队列的容量
*/
public CircularQueue(Integer capacity) {
// 默认头部元素设置为数组的第0号元素
this.head = 0;
// 默认尾部元素设置为数组的第0号元素
this.tail = 0;
this.capacity = capacity;
this.elements = (T[]) new Object[capacity];
}
/**
* 构造一个默认容量的环形队列,默认容量3600
*/
public CircularQueue() {
// 默认头部元素设置为数组的第0号元素
this.head = 0;
// 默认尾部元素设置为数组的第0号元素
this.tail = 0;
this.capacity = DEFAULT_CAPACITY;
this.elements = (T[]) new Object[DEFAULT_CAPACITY];
}
/**
* 判断队列是否已满
*
* @return boolean
*/
public boolean isFull() {
// 头部和尾部没有指向同一个元素 并且 队列的元素个数和初始化大小相同
return !this.head.equals(this.tail) && this.size().equals(this.capacity);
}
/**
* 判断队列是否为空
*
* @return boolean
*/
public boolean isEmpty() {
// 头部和尾部没有指向同一个元素
return this.head.equals(this.tail);
}
/**
* 添加一个队列元素
*
* @param t 队列元素
*/
public void add(T t) {
// 判断队列是否已满
if (isFull()) {
throw new RuntimeException("环形队列已满");
}
// 1.尾部对容量求余
// 2.设置元素内容
// 3.尾部向后移动一个角标
this.elements[this.tail % this.capacity] = t;
this.tail++;
}
/**
* 删除一个队列头部元素
*/
public void remove() {
// 判断队列是否已空
if (isEmpty()) {
throw new RuntimeException("环形队列无元素");
}
// 1.头部元素置为空
// 2.头部向后移动一个角标
this.elements[this.head % this.capacity] = null;
this.head++;
}
/**
* 获取一个队列头部元素
*/
public T get() {
// 判断队列是否已空
if (isEmpty()) {
throw new RuntimeException("环形队列无元素");
}
// 获取头部元素
return this.elements[this.head % this.capacity];
}
/**
* 获取一个队列头部元素
*/
public T getAndRemove() {
// 获取一个头部元素,并临时存储
T t = this.get();
// 删掉头部元素
this.remove();
// 返回数据
return t;
}
/**
* 获取队列元素个数
*
* @return 队列容量
*/
public Integer size() {
return this.tail - this.head;
}
}
QueueTest.java
package com.example.demo.circularqueue;
/**
* @author sunweidong
*/
public class QueueTest {
public static void main(String[] args) {
add();
// addAndError();
remove();
}
private static void add() {
CircularQueue<String> strQueue = new CircularQueue<>();
for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
strQueue.add(i + "");
}
System.out.println("isFull=" + strQueue.isFull());
System.out.println("isEmpty=" + strQueue.isEmpty());
System.out.println("size=" + strQueue.size());
System.out.println();
}
private static void addAndError() {
CircularQueue<Integer> intQueue = new CircularQueue<>(100);
for (int i = 0; i < 100; i++) {
intQueue.add(i);
}
System.out.println("isFull = " + intQueue.isFull());
System.out.println("isEmpty = " + intQueue.isEmpty());
System.out.println("size = " + intQueue.size());
System.out.println();
// 环形队列已满
intQueue.add(100);
}
private static void remove() {
CircularQueue<String> strQueue = new CircularQueue<>();
for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
strQueue.add(i + "");
}
System.out.println("add---isFull = " + strQueue.isFull());
System.out.println("add---isEmpty = " + strQueue.isEmpty());
System.out.println("add---size = " + strQueue.size());
System.out.println();
for (int i = 0; i < CircularQueue.DEFAULT_CAPACITY; i++) {
strQueue.remove();
}
System.out.println("remove---isFull = " + strQueue.isFull());
System.out.println("remove---isEmpty = " + strQueue.isEmpty());
System.out.println("remove---size = " + strQueue.size());
}
}