我是靠谱客的博主 诚心盼望,这篇文章主要介绍使用数组模拟队列以及优化环形队列详情请看代码注释:环形队列:,现在分享给大家,希望可以做个参考。

详情请看代码注释:

复制代码
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
package com.example; import java.util.Arrays; import java.util.Scanner; /** * @Author Gao Xiaozhuang * @Date 2020/7/27 18:40 * @WARN 要写注释 * @Version 1.0 * @Description */ public class ArrayQueueDemo { public static void main(String[] args) { ArrayQueue arrayQueue = new ArrayQueue(3); char key = ' '; Scanner sc = 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 = sc.next().charAt(0); switch (key) { case 's': arrayQueue.showQueue(); break; case 'a': System.out.println("请输入:"); int value = sc.nextInt(); arrayQueue.addQueue(value); break; case 'g': try { System.out.println("取出的数据是:" + arrayQueue.getQueue()); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { System.out.println("取出的数据是:" + arrayQueue.headQueue()); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': 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("队列为空,不可遍历"); } System.out.println(Arrays.toString(arr)); } 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
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
package com.example; import java.util.Arrays; import java.util.Scanner; /** * @Author Gao Xiaozhuang * @Date 2020/7/27 19:27 * @WARN 要写注释 * @Version 1.0 * @Description环形队列 思路: * 1.front变量的含义做一个调整:front就指向队列的第一个元素,也就是arr[front]就是队列的第一个元素 * front初始值为 0 rear初始值为 0 * 2.rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定 * 3.当队列满时,条件是(rear+1)%maxSize=front * 4.当队列空时,rear=front * 5.队列中有效的数据的个数为 (rear+maxSize-front)%maxSize */ public class CircleQueueDemo { public static void main(String[] args) { CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4); char key = ' '; Scanner sc = 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 = sc.next().charAt(0); switch (key) { case 's': circleArrayQueue.showQueue(); break; case 'a': System.out.println("请输入:"); int value = sc.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { System.out.println("取出的数据是:" + circleArrayQueue.getQueue()); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { System.out.println("取出的数据是:" + circleArrayQueue.headQueue()); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': loop = false; break; default: break; } } System.out.println("程序退出"); } } class CircleArrayQueue { private int maxSize; private int front; private int rear; private int[] arr; /** * 创建队列构造器 */ public CircleArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } public boolean isFull() { return (rear + 1) % maxSize == front; } public boolean isEmpty() { return rear == front; } public void addQueue(int n) { if (isFull()) { System.out.println("队列满,不可加入"); return; } arr[rear] = n; rear = (rear + 1) % maxSize; } public int getQueue() { if (isEmpty()) { throw new RuntimeException("队列为空,不能取数据"); } int value = arr[front]; front = (front + 1) % maxSize; return value; } public void showQueue() { if (isEmpty()) { System.out.println("队列为空,不可遍历"); } for (int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%dn", i % maxSize, arr[i % maxSize]); } } /** * 求出当前队列有效数据的个数 * * @return */ public int size() { return (rear + maxSize - front) % maxSize; } public int headQueue() { if (isEmpty()) { throw new RuntimeException("队列为空,没有队头"); } return arr[front]; } }

 

 

最后

以上就是诚心盼望最近收集整理的关于使用数组模拟队列以及优化环形队列详情请看代码注释:环形队列:的全部内容,更多相关使用数组模拟队列以及优化环形队列详情请看代码注释内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部