1. 数组模拟队列
使用数组实现队列的先进先出等操作
示例代码:
public class Queue {
//队列的最大容量
private int maxSize;
//队列中第一个数据的索引
private int front;
//队列中最后一个数据的索引
private int rear;
//模拟队列的数组
private int[] arr;
//构造函数初始化队列
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
//初始队列没有数据
front = -1;
rear = -1;
}
//判断队列是否已满
public boolean isFull() {
return rear == maxSize - 1;
}
//判断队列是否为空
public boolean isEmpty() {
return front == rear;
}
//往队列中添加数据
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已满,无法添加数据!");
return;
}
rear++;
arr[rear] = n;
}
//从队列中获取数据
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,无法获取数据!");
}
front++;
return arr[front];
}
//获取队头数据
public int getHead() {
if (isEmpty()) {
throw new RuntimeException("队列为空,无法获取数据!");
}
return arr[front + 1];
}
//打印队列
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空!");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
}
2. 数组模拟环形队列
上述数组实现的队列,只能使用一次,无法真正实现取出数据的效果,所以对其进行优化,使用数组通过取模(%)操作实现环形队列。
在环形队列中,front始终为队列第一个元素的索引;
而rear则始终为队列最后一个元素的下一个索引;
所以,环形队列最大容量(maxSize)为4时,如果队列中装了3个元素,则当前队列已满;
示例图:
示例代码:
public class Queue {
//队列的最大容量
private int maxSize;
//队列中第一个数据的索引
private int front;
//队列中最后一个数据的索引
private int rear;
//模拟队列的数组
private int[] arr;
//构造函数初始化队列,因为int类型默认初始值为0,所以不需要给front和rear赋初值
public CircleArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
//判断队列是否已满
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
//判断队列是否为空
public boolean isEmpty() {
return front == rear;
}
//往队列中添加数据
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已满,无法添加数据!");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
//从队列中获取数据
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,无法获取数据!");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
//获取队头数据
public int getHead() {
if (isEmpty()) {
throw new RuntimeException("队列为空,无法获取数据!");
}
return arr[front];
}
//打印队列
public void showQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
//求出当前环形队列中有效数据的个数
public int size() {
return (rear + maxSize - front) % maxSize;
}
3. 测试类
测试类代码:
public class Test {
public static void main(String[] args) {
CircleQueue arrayQueue = new CircleQueue(4);
char key = ' ';
Scanner in = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列中取出数据");
System.out.println("h(head):查看队列头的数据");
System.out.println("请选择您要进行的操作(s/e/a/g/h):");
key = in.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数据:");
arrayQueue.addQueue(in.nextInt());
break;
case 'g':
try {
int value = arrayQueue.getQueue();
System.out.printf("取出的数据为:%d\n", value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int head = arrayQueue.getHead();
System.out.printf("队列头的数据为:%d\n", head);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
in.close();
loop = false;
System.out.println("程序退出!");
break;
default:
break;
}
}
}
}