Skip to content

Commit d81a125

Browse files
author
chenweijie
committed
添加队列的实现
1 parent 9c69823 commit d81a125

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.chen.dataStructure.queue;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-03-07 12:13 AM
6+
*/
7+
public class MyQueue {
8+
9+
private Object[] queArray;
10+
11+
/**
12+
* 队列总大小
13+
*/
14+
private int maxSize;
15+
16+
/**
17+
* 前端
18+
*/
19+
private int front;
20+
21+
/**
22+
* 后端
23+
*/
24+
private int fear;
25+
26+
/**
27+
* 队列中实际元素个数
28+
*/
29+
private int nItems;
30+
31+
public MyQueue(int s) {
32+
this.maxSize = s;
33+
this.queArray = new Object[maxSize];
34+
front = 0;
35+
fear = -1;
36+
nItems = 0;
37+
}
38+
39+
40+
/**
41+
* 返回队列的大小
42+
*
43+
* @return
44+
*/
45+
public int getSize() {
46+
return nItems;
47+
}
48+
49+
/**
50+
* 队列是否为空
51+
*
52+
* @return
53+
*/
54+
public boolean isEmpty() {
55+
return nItems == 0;
56+
}
57+
58+
59+
/**
60+
* 队列是否满了
61+
*
62+
* @return
63+
*/
64+
public boolean isFull() {
65+
return maxSize == nItems;
66+
}
67+
68+
/**
69+
* 查看队头元素
70+
*
71+
* @return
72+
*/
73+
public Object peekFront() {
74+
return queArray[front];
75+
}
76+
77+
78+
/**
79+
* 移除元素
80+
*
81+
* @return
82+
*/
83+
public Object remove() {
84+
85+
Object removeValue = null;
86+
if (!isEmpty()) {
87+
removeValue = peekFront();
88+
front++;
89+
if (front == maxSize) {
90+
front = 0;
91+
}
92+
nItems--;
93+
return removeValue;
94+
}
95+
return removeValue;
96+
}
97+
98+
99+
100+
/**
101+
* 队列中新增元素
102+
*
103+
* @param value
104+
*/
105+
public void insert(Object value) {
106+
107+
if (isFull()) {
108+
System.out.println("队列已满!!!");
109+
} else {
110+
if (fear == maxSize - 1) {
111+
fear = -1;
112+
}
113+
//队尾指针加1,然后在队尾指针处插入新的数据
114+
queArray[++fear] = value;
115+
nItems++;
116+
}
117+
}
118+
119+
120+
public static void main(String[] args) {
121+
122+
MyQueue myQueue = new MyQueue(4);
123+
124+
myQueue.insert(10);
125+
myQueue.insert(20);
126+
myQueue.insert(30);
127+
myQueue.insert(40);
128+
129+
//获取队头元素
130+
System.out.println("myQueue.peekFront():" + myQueue.peekFront());
131+
132+
//移除元素
133+
myQueue.remove();
134+
135+
//插入元素
136+
myQueue.insert(50);
137+
myQueue.insert(60);
138+
139+
for (Object a : myQueue.queArray) {
140+
System.out.println("a:" + a);
141+
}
142+
}
143+
144+
145+
146+
147+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.chen.dataStructure.queue;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-03-07 12:51 AM
6+
*/
7+
public class PriorityQue {
8+
9+
10+
private int maxSize;
11+
12+
private int[] priQueArray;
13+
14+
private int nItems;
15+
16+
17+
public PriorityQue(int s) {
18+
this.maxSize = s;
19+
this.priQueArray = new int[maxSize];
20+
this.nItems = 0;
21+
}
22+
23+
24+
/**
25+
* 元素是否满了
26+
*
27+
* @return
28+
*/
29+
public boolean isFull() {
30+
return maxSize == nItems;
31+
}
32+
33+
/**
34+
* 队列是否为空
35+
*
36+
* @return
37+
*/
38+
public boolean isEmpty() {
39+
return nItems == 0;
40+
}
41+
42+
43+
/**
44+
* 查看优先级最高的元素
45+
*
46+
* @return
47+
*/
48+
public int peekMin() {
49+
return priQueArray[nItems - 1];
50+
}
51+
52+
public void insert(int value) {
53+
54+
if (maxSize == nItems) {
55+
System.out.println("队列已满!");
56+
}
57+
58+
int j = 0;
59+
60+
if (nItems == 0) {
61+
priQueArray[nItems++] = value;
62+
} else {
63+
//选择的排序方法是插入排序,按照从大到小的顺序排列,越小的越在队列的顶端
64+
while (j >= 0 && value > priQueArray[j]) {
65+
priQueArray[j + 1] = priQueArray[j];
66+
j--;
67+
}
68+
}
69+
priQueArray[j + 1] = value;
70+
nItems++;
71+
}
72+
73+
//移除数据,由于是按照大小排序的,所以移除数据我们指针向下移动
74+
//被移除的地方由于是int类型的,不能设置为null,这里的做法是设置为 -1
75+
public int remove() {
76+
77+
int k = nItems - 1;
78+
int value = priQueArray[k];
79+
priQueArray[k] = -1;
80+
nItems--;
81+
return value;
82+
}
83+
84+
85+
}

0 commit comments

Comments
 (0)