-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueue.java
More file actions
39 lines (33 loc) · 872 Bytes
/
Copy pathMyQueue.java
File metadata and controls
39 lines (33 loc) · 872 Bytes
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
package cx.Linear;
public class MyQueue {
int[] elements;
public MyQueue() {
elements = new int[0];
}
//入队
public void add(int element) {
int[] newArr = new int[elements.length + 1];
for (int i = 0; i < elements.length; i++) {
newArr[i] = elements[i];
}
newArr[elements.length] = element;
elements = newArr;
}
//出队
public int poll() {
if (elements.length == 0) {
throw new RuntimeException("queue is empty");
}
int element = elements[0];
int[] newArr=new int[elements.length-1];
for (int i=0;i<newArr.length;i++){
newArr[i]=elements[i+1];
}
elements=newArr;
return element;
}
//判断是否为空
public boolean isEmpty(){
return elements.length==0;
}
}