|
| 1 | +public class LoopQueue<E> implements Queue<E> { |
| 2 | + |
| 3 | + private E[] data; |
| 4 | + private int front, tail; |
| 5 | + private int size; |
| 6 | + |
| 7 | + public LoopQueue(int capacity) { |
| 8 | + data = (E[]) new Object[capacity + 1]; |
| 9 | + front = 0; |
| 10 | + tail = 0; |
| 11 | + size = 0; |
| 12 | + } |
| 13 | + |
| 14 | + public LoopQueue() { |
| 15 | + this(10); |
| 16 | + } |
| 17 | + |
| 18 | + public int getCapacity() { |
| 19 | + return data.length - 1; |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + @Override |
| 24 | + public int getSize() { |
| 25 | + return size; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean isEmpty() { |
| 30 | + return front == tail; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void enqueue(E e) { |
| 35 | + if ((tail + 1) % data.length == front) { |
| 36 | + resize(getCapacity() * 2); |
| 37 | + } |
| 38 | + |
| 39 | + data[tail] = e; |
| 40 | + tail = (tail + 1) % data.length; |
| 41 | + size++; |
| 42 | + } |
| 43 | + |
| 44 | + private void resize(int newCapacity) { |
| 45 | + E[] newData = (E[]) new Object[newCapacity + 1]; |
| 46 | + for (int i = 0; i < size; i++) { |
| 47 | + newData[i] = data[(i + front) % data.length]; |
| 48 | + } |
| 49 | + data = newData; |
| 50 | + front = 0; |
| 51 | + tail = size; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public E dequeue() { |
| 56 | + if (isEmpty()) { |
| 57 | + throw new IllegalArgumentException("Cannot dequeue from an empty queue."); |
| 58 | + } |
| 59 | + E ret = data[front]; |
| 60 | + data[front] = null; |
| 61 | + front = (front + 1) % data.length; |
| 62 | + size--; |
| 63 | + if (size == getCapacity() / 4 && getCapacity() / 2 != 0) { |
| 64 | + resize(getCapacity() / 2); |
| 65 | + } |
| 66 | + return ret; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public E getFront() { |
| 71 | + if (isEmpty()) { |
| 72 | + throw new IllegalArgumentException("Cannot dequeue from an empty queue."); |
| 73 | + } |
| 74 | + return data[front]; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Override |
| 79 | + public String toString() { |
| 80 | + StringBuilder res = new StringBuilder(); |
| 81 | + res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity())); |
| 82 | + res.append("front ["); |
| 83 | + for (int i = front; i != tail; i = (i + 1) % data.length) { |
| 84 | + res.append(data[i]); |
| 85 | + if ((i + 1) % data.length != tail) { |
| 86 | + res.append(", "); |
| 87 | + } |
| 88 | + } |
| 89 | + res.append("] tail"); |
| 90 | + return res.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) { |
| 94 | + LoopQueue<Integer> queue = new LoopQueue<>(); |
| 95 | + for (int i = 0; i < 10; i++) { |
| 96 | + queue.enqueue(i); |
| 97 | + System.out.println(queue); |
| 98 | + |
| 99 | + if (i % 3 == 2) { |
| 100 | + queue.dequeue(); |
| 101 | + System.out.println(queue); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments