隊列——循環隊列
阿新 • • 發佈:2019-03-07
str java ring cap 數據 head capacity lse dequeue
這次看下循環隊列,也是使用數組實現,但是避免了數據的搬動。
如有不對請提出,共同提高,謝謝!!!
public class CircularQueue { private String[] items; private int n = 0; private int head = 0; private int tail = 0; public CircularQueue(int capacity) { items = new String[capacity]; n = capacity; } public boolean enqueue(String item) { if ((tail + 1) % n == head) return false; items[tail] = item; tail = (tail + 1) % n; return true; } public String dequeue() { if (head == tail) return null; String ret = items[head]; head = (head + 1) % n; return ret; } }
隊列——循環隊列