用数组模拟一个队列,缺点是不能重复使用
一、代码:
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("h(head):查看队列头的数据");
System.out.println("e(exit):退出队列");
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) {
System.out.println(e.getMessage());
}
break;
case 'h'://查看队列头的数据。当队列为空时,有可能会抛出异常
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是:%d\n", res);
}catch(Exception e){
System.out.println(e.getMessage());
}
break;
case 'e'://退出队列
scanner.close();
loop = false;
break;
default:
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 n) {
if (isFull()) {
System.out.println("队列已满,不能加入数据!");
return;
}
rear++;
arr[rear] = n;
}
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):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
s
队列为空,没有数据!
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
g
队列为空,不能取数据!
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
h
队列为空,没有数据!
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
a
输入一个数:
10
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
s
arr[0] = 10
arr[1] = 0
arr[2] = 0
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
a
输入一个数:
20
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
s
arr[0] = 10
arr[1] = 20
arr[2] = 0
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
a
输入一个数:
30
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
s
arr[0] = 10
arr[1] = 20
arr[2] = 30
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
g
取出的数据是:10
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
s
arr[0] = 10
arr[1] = 20
arr[2] = 30
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
h
队列头的数据是:20
s(show):显示队列
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头的数据
e(exit):退出队列
e
程序已退出!