import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(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):查看队列头的数据");
key = scanner.next().charAt(0);
switch(key){
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("输入一个数");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.printf("队列头的数据是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出");
}
}
class ArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;
rear = -1;
}
public boolean isFull(){
return rear == maxSize - 1;
}
public boolean isEmpty(){
return rear == front;
}
public void addQueue(int num){
if(isFull()){
System.out.println("队列已满");
return;
}
rear++;
arr[rear] = num;
}
public int getQueue(){
if(isEmpty()){
throw new RuntimeException("队列已空");
}
front++;
return arr[front];
}
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]);
}
}
public int headQueue(){
if(isEmpty()){
throw new RuntimeException("队列空,无数据");
}
return arr[front + 1];
}
}
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
队列空,无法遍历
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
a
输入一个数
1
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
arr[0] = 1
arr[1] = 0
arr[2] = 0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
a
输入一个数
2
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
arr[0] = 1
arr[1] = 2
arr[2] = 0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
a
输入一个数
3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
arr[0] = 1
arr[1] = 2
arr[2] = 3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
h
队列头的数据是1
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
g
取出的数据是1
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
h
队列头的数据是2
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
arr[0] = 1
arr[1] = 2
arr[2] = 3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
g
取出的数据是2
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
h
队列头的数据是3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
arr[0] = 1
arr[1] = 2
arr[2] = 3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
g
取出的数据是3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
h
队列空,无数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
s
队列空,无法遍历
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列里面取出数据
h(head):查看队列头的数据
弊端:只能用一次,无法复用