我是靠谱客的博主 直率汽车,这篇文章主要介绍简单循环数组实现队列,现在分享给大家,希望可以做个参考。

最近在看数据结构与算法之类的书,看到喜欢的小东西随手记一下。

复制代码
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
/** * 简单循环数组构造队列结构 * @author Administrator@2018年12月12日 下午8:30:47 */ public class ArrayQueue { public int capacity; public int[] arrayQueue; public int front; public int rear; public ArrayQueue(int size) { capacity = size; arrayQueue = new int[size]; front = -1; rear = -1; } /** * 队列是否为空 * @return * @author Administrator@2018年12月12日 下午8:33:59 */ public boolean isEmpty() { return (front == -1); } /** * 判断队列是否已经满了 * @return * @author Administrator@2018年12月12日 下午8:35:02 */ public boolean isFull() { return ((rear + 1)%capacity == front); } /** * 删除队列首元素 * @return * @author Administrator@2018年12月12日 下午8:36:33 */ public int arrayPoll() { if(isEmpty()) { throw new NullPointerException("队列已经空了!"); } int data = arrayQueue[front]; if(front == rear) { front = rear = -1; }else { front = (front + 1) % capacity; } return data; } /** * 元素入队 * @param element * @return * @author Administrator@2018年12月12日 下午8:37:09 */ public int arrayOffer(int element) { if(isFull()) { throw new IndexOutOfBoundsException("队列已满!"); } rear = (rear + 1) % capacity; arrayQueue[rear] = element; if(front == -1) { front = rear; } return arrayQueue[front]; } /** * 获取队首元素 * @return * @author Administrator@2018年12月12日 下午8:37:53 */ public int arrayPeek() { if(isEmpty()) { throw new NullPointerException("队列已经空了!"); } return arrayQueue[front]; } /** * 获取队列元素的个数 * @return * @author Administrator@2018年12月12日 下午8:48:18 */ public int arrayQueueCapicity() { return (rear - front + 1 + capacity) % capacity; } }

最后

以上就是直率汽车最近收集整理的关于简单循环数组实现队列的全部内容,更多相关简单循环数组实现队列内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部