-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayQueue.java
60 lines (49 loc) · 1.12 KB
/
ArrayQueue.java
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
public class ArrayQueue<E> {
private ArrayList<E> list;
public ArrayQueue() {
list = new ArrayList();
}
public ArrayQueue(int initCapacity) {
list = new ArrayList(initCapacity);
}
public boolean empty() {
return list.size() == 0;
}
/**
* 出队
* @param e
*/
public void offer(E e) {
list.add(e);
}
/**
* 入队
* @return
*/
public E poll() {
return list.remove(0);
}
/**
* 查看队列头部元素
* @return
*/
public E peek() {
return list.get(0);
}
public int size() {
return list.size();
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("{ capacity: " + list.capacity() + ", size: " + list.size() + ", item: head -> [");
for (int i = 0; i < list.size(); i++) {
result.append(list.get(i));
if (i != list.size() - 1) {
result.append(", ");
}
}
result.append("] -> tail }");
return result.toString();
}
}