Skip to content

Commit b0c415e

Browse files
committed
add LoopQueue
1 parent 4df3c87 commit b0c415e

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

Queue/src/LoopQueue.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

Queue/src/Main.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Random;
2+
3+
public class Main {
4+
5+
//
6+
private static double testQueue(Queue<Integer> q, int opCount) {
7+
long startTime = System.nanoTime();
8+
9+
Random random = new Random();
10+
for (int i = 0; i < opCount; i++) {
11+
q.enqueue(random.nextInt(Integer.MAX_VALUE));
12+
}
13+
for (int i = 0; i < opCount; i++) {
14+
q.dequeue();
15+
}
16+
17+
long endTime = System.nanoTime();
18+
19+
return (endTime - startTime) / 1000000000.0;
20+
}
21+
22+
public static void main(String[] args) {
23+
int opCount = 100000;
24+
25+
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
26+
double time1 = testQueue(arrayQueue, opCount);
27+
System.out.println("ArrayQueue, time: " + time1 + " s");
28+
29+
LoopQueue<Integer> loopQueue = new LoopQueue<>();
30+
double time2 = testQueue(loopQueue, opCount);
31+
System.out.println("LoopQueue, time: " + time2 + " s");
32+
}
33+
}

0 commit comments

Comments
 (0)