代码实现
编写一个ArrayQueue类
复制代码
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//使用数组模拟队列-编写一个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; // 指向队列尾,rear是指向队列尾的位置 } // 判断队列是否满 public boolean isFull() { return rear == maxSize-1; } // 判断队列是否为空 public boolean isEmpty() { return front == rear; } // 添加数据到队列 public void addQueue(int n) { // 判断队列是否满 if (isFull()) { System.out.println("队列满,不能加入数据~"); return; } rear++; // 将 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]=%dn", i, arr[i]); } } // 显示队列的头数据, 注意不是取出数据 public int headQueue() { // 判断 if (isEmpty()) { throw new RuntimeException("队列空,没有数据~"); } return arr[front+1]; } }
测试代码
复制代码
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
68package com.atguigu.queue; import java.util.Scanner; /** * @author WHT * @create 2021-06-01 11:01 */ 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("取出的数据是%dn", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = queue.headQueue(); System.out.printf("队列头的数据是%dn", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出~~"); } }
运行结果
复制代码
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
116D:javajdk-11.0.11binjava.exe "-javaagent:D:IntelliJ IDEA 2020.1.1libidea_rt.jar=4157:D:IntelliJ IDEA 2020.1.1bin" -Dfile.encoding=UTF-8 -classpath E:WHTProjectJavaDataStructuresoutproductionDataStructures com.atguigu.queue.ArrayQueueDemo s(show): 显示队列 a(add): 添加数据到队列 g(get): 从队列取出数据 h(head): 查看队列头的数据 e(exit): 退出程序 s 队列空,没有数据~ 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): 退出程序 a 请输入一个数: 40 队列满,不能加入数据~ s(show): 显示队列 a(add): 添加数据到队列 g(get): 从队列取出数据 h(head): 查看队列头的数据 e(exit): 退出程序 h 队列头的数据是10 s(show): 显示队列 a(add): 添加数据到队列 g(get): 从队列取出数据 h(head): 查看队列头的数据 e(exit): 退出程序 h 队列头的数据是10 s(show): 显示队列 a(add): 添加数据到队列 g(get): 从队列取出数据 h(head): 查看队列头的数据 e(exit): 退出程序 g 取出的数据是10 s(show): 显示队列 a(add): 添加数据到队列 g(get): 从队列取出数据 h(head): 查看队列头的数据 e(exit): 退出程序 h 队列头的数据是20 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): 退出程序 e 程序退出~~ Process finished with exit code 0
最后
以上就是暴躁马里奥最近收集整理的关于尚硅谷Java数据结构与算法——012数组模拟队列的全部内容,更多相关尚硅谷Java数据结构与算法——012数组模拟队列内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复