简单
public static class ArrayQueue {
private Integer[] arr;
private Integer size;
private Integer start;
private Integer end;
public ArrayQueue(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size = 0;
start = 0;
end = 0;
}
public Integer peek() {
if (size == 0) {
return null;
}
return arr[start];
}
public void push(int num) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("The queue is full");
}
size++;
arr[end] = num;
end = end == arr.length - 1 ? 0 : end + 1;
}
public Integer poll() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("The queue is empty");
}
size--;
int tmp = start;
start = start == arr.length - 1 ? 0 : start + 1;
return arr[tmp];
}
}
复杂
package com.liaoxiang.queue;
import java.util.Scanner;
/**
* @auther Mr.Liao
* @date 2019/7/5 16:49
*/
public class CircleArrayQueueDemo {
public static void main(String[] args) {
// 创建一个环形队列
CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3
char key = ' '; // 接收用户输入
Scanner scanner = 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("r(showReal): 显示原数组");
key = scanner.next().charAt(0);// 接收一个字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a': // 添加一个数
System.out.println("输出一个数");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g': // 取出数据
try {
int res = queue.getQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h': // 查看队列头的数据
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'r':
queue.realShow();
break;
case 'e': // 退出
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出!");
}
}
class CircleArray {
private int maxSize; // 表示数组的最大容量
private int front; // 队列头
private int rear; // 队列尾
private int[] arr; // 该数组用于存放数据, 模拟队列
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
}
/**
* 判断队列是否满、队列尾追上队列头
* @return
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加数据到队列
* @param n
*/
public void addQueue(int n) {
// 判断队列是否满
if (isFull()) {
System.out.println("队列已满!");
return;
}
//直接将数据加入
arr[rear] = n;
System.out.printf("添加的数据:arr[%d] = %d\n",rear,n);
//将 rear 后移, 这里必须考虑取模
rear = (rear + 1) % maxSize;
System.out.println("添加数据后rear="+rear);
}
/**
* 获取队列的数据, 出队列
* @return
*/
public int getQueue() {
if (isEmpty()) {
// 通过抛出异常
throw new RuntimeException("队列为空!");
}
// 这里需要分析出front是指向队列的第一个元素
// 1. 先把 front 对应的值保留到一个临时变量
// 2. 取出后将front后移, 考虑取模
// 3. 将临时保存的变量返回
int value = arr[front];
System.out.printf("取出的数据:arr[%d] = %d\n",front,value);
front = (front + 1) % maxSize;
System.out.println("取出数据后front="+front);
return value;
}
/**
* 显示队列的所有数据
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空!");
return;
}
// 思路:从front开始遍历,遍历多少个元素
System.out.println("rear="+rear+", front="+front);
for (int i = front; i < front + size() ; i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
/**
* 求出当前队列有效数据的个数
*/
public int size() {
System.out.println();
return (rear + maxSize - front) % maxSize;
}
/**
* 显示队列的头数据, 注意不是取出数据
* @return
*/
public int headQueue() {
// 判断
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
System.out.printf("队列头位置:arr[%d]=%d\n",front,arr[front]);
return arr[front];
}
/**
* 显示原数组
*/
public void realShow(){
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
}
运行分析:
①先向队列中添加三个数据
a
请输入要添加的数据:
10
添加的数据:arr[0] = 10
添加数据后rear=1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
a
请输入要添加的数据:
20
添加的数据:arr[1] = 20
添加数据后rear=2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
a
请输入要添加的数据:
30
添加的数据:arr[2] = 30
添加数据后rear=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
s
rear=3, front=0
arr[0]=10
arr[1]=20
arr[2]=30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
r
arr[0]=10
arr[1]=20
arr[2]=30
arr[3]=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
此时队列的情况如下:
②取出一个数据再添加一个数据:
g
取出的数据:arr[0] = 10
取出数据后front=1
取出的数据是10
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
s
rear=3, front=1
arr[1]=20
arr[2]=30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
r
arr[0]=10
arr[1]=20
arr[2]=30
arr[3]=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
a
请输入要添加的数据:
40
添加的数据:arr[3] = 40
添加数据后rear=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
s
rear=0, front=1
arr[1]=20
arr[2]=30
arr[3]=40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
r
arr[0]=10
arr[1]=20
arr[2]=30
arr[3]=40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
③再取出一个,并添加一个
h
队列头位置:arr[1]=20
队列头的数据是20
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
g
取出的数据:arr[1] = 20
取出数据后front=2
取出的数据是20
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
s
rear=0, front=2
arr[2]=30
arr[3]=40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
a
请输入要添加的数据:
50
添加的数据:arr[0] = 50
添加数据后rear=1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
s
rear=1, front=2
arr[2]=30
arr[3]=40
arr[0]=50
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
r
arr[0]=50
arr[1]=20
arr[2]=30
arr[3]=40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
r(showReal): 显示原数组
过程分析: