-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
58 lines (50 loc) · 1.15 KB
/
ArrayQueue.java
File metadata and controls
58 lines (50 loc) · 1.15 KB
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
package ds.queue;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/11/19
* <p>
* 用数组实现的队列
*/
public class ArrayQueue implements IQueue {
private String[] items;
private int n = 0;
private int head = 0;
private int tail = 0;
public ArrayQueue(int capacity) {
items = new String[capacity];
this.n = capacity;
}
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(3);
queue.enqueue("3");
queue.enqueue("2");
queue.printAll();
queue.dequeue();
queue.printAll();
queue.dequeue();
queue.printAll();
}
@Override
public boolean enqueue(String item) {
if (tail == n) {
return false;
}
items[tail] = item;
++tail;
return true;
}
@Override
public String dequeue() {
if (head == tail) {
return null;
}
return items[head++];
}
@Override
public void printAll() {
for (int i = head; i < tail; i++) {
System.out.print(items[i] + " ");
}
System.out.println();
}
}