-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
128 lines (111 loc) · 2.71 KB
/
ArrayQueue.java
File metadata and controls
128 lines (111 loc) · 2.71 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
package com.briup.hsp.shujijiegou.day1.quene;
import java.util.Scanner;
public class ArrayQueue {
public static void main(String[] args) {
ArrayQ arrq =new ArrayQ(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':
arrq.showQuene();
break;
case 'a':
System.out.println("输入一个数");
int value = scanner.nextInt();
arrq.addQuene(value);
break;
case 'g':
try {
int result = arrq.getQuene();
System.out.printf("取出的数据是%d\n",result);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int hResult = arrq.headQuene();
System.out.printf("队列头的数据是%d\n",hResult);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
//编写ArrayQ类 数组模拟队列
class ArrayQ{
private int maxSize;//表示数组最大容量
private int front;//指向队列头
private int rare;//指向队列尾
private int[] arr;//该数组用于存放数据,模拟队列
//创建对象的构造器
public ArrayQ(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;//指向对象头部的前一个位置
rare = -1;//指向队列尾的数据 和位置
}
//判断队列是否满
public boolean isFull() {
return rare == maxSize-1 ;
}
//判断队列是否为空
public boolean isEmpty() {
return front == rare;
}
//给队列添加数据
public void addQuene(int n) {
//判断队列是否满
if(isFull()) {
System.out.println("队列已满");
return;
}
rare++;
arr[rare] = n;
}
//出队列
public int getQuene() {
if(isEmpty()) {
throw new RuntimeException("队列空, 不能取数据");
}
front++;
return arr[front];
}
//显示队列的所有数据
public void showQuene() {
//队列为空 无法遍历
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 headQuene() {
if(isEmpty()) {
throw new RuntimeException("为空");
}
return arr[front+1];
}
}