-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArrayQueueDemo.java
More file actions
132 lines (116 loc) · 2.83 KB
/
ArrayQueueDemo.java
File metadata and controls
132 lines (116 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.atguigu.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
//测试
ArrayQueue queue = new ArrayQueue(3);
char key = ' ';//接收用户输入
java.util.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("程序退出");
}
}
//使用数组模拟队列编写一个ArrayQueue类
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;//指向队列头部,分析出front是指向队列头的前一个位置
rear = -1;//指向队列尾,指向队列尾的数据(即队列最后一个数据)
}
//判断队列是否为空
public boolean isEmpty() {
return front == rear;
}
//判断队列是否满
public boolean isFull() {
return rear == maxSize-1;
}
//添加数据到队列
public void addQueue(int n) {
if(isFull()) {
System.out.println("队列满,不能加入数据");
return;
}
rear++;//尾指针后移
arr[rear]=n;
}
//获取队列的数据,出队列
public int getQueue() {
//判空
if(isEmpty()) {
//抛出异常
throw new RuntimeException("队列空,不能取数据");
}
front++;//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];//注意:front指向的是队列头的前一个数据。(更小的一个位置)
}
}