-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueueDemo.java
More file actions
169 lines (123 loc) · 4.18 KB
/
Copy pathArrayQueueDemo.java
File metadata and controls
169 lines (123 loc) · 4.18 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.CodeRoot.queue.ArrayQueueDemo;
/**
*
*
*/
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args){
/**
* 数组模拟队列
* 1.先写一个ArrayQueue类
*/
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("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("h(head):查看队列头数据");
System.out.println("选择操作:");
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里执行时,遇到异常,则直接进入catch,用e.getMessage()打印错误信息
*/
try {
int res=queue.getQueue();
System.out.println("取出的数是:"+res+"\n");
}catch (Exception e){
System.out.println(e.getMessage());//自动打印错误信息
}
break;
case 'h':
try {
int res=queue.headQueue();
System.out.println("队列头部的数是:"+res+"\n");
}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 front;//队列头部的前一个下标 front!=头部
* private int rear;//队列尾部下标 rear==尾部
*/
//1.属性
private int maxSize;//最大容量
private int front;//队列头部的前一个下标 front!=头部
private int rear;//队列尾部下标 rear==尾部
private int[] arr;//存放数据,模拟队列
//2.创建队列的构造器
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr=new int[maxSize];
front=-1;//指向队列头部的那个数据的前一个位置:比如头部下标为0,则front=-1
rear=-1;//指向队列尾部的那个数据
}
//3.判断队列是否满了
public boolean isFull(){
return rear==maxSize-1;
}
//4.判断是否为空队列
public boolean isEmpty(){
return front==rear;
}
//5.添加数据到队列
public void addQueue(int n){
if(isFull()) {
System.out.println("不能再添加数据了");
return;
}
rear++;
arr[rear]=n;
}
//6.出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,不能取数据");//throw就相当于打印这句话然后return
}
front++;//front后移,这样就方便取出头部
return arr[front];
}
//7.显示队列
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];
}
}