-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularQueue.java
79 lines (68 loc) · 1.93 KB
/
CircularQueue.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.NoSuchElementException;
class CircularQueue {
private String[] queue;
private int n;
private int first;
private int last;
public CircularQueue() {
queue = new String[2];
n = 0;
first = 0;
last = 0;
}
public boolean isEmpty() {
return n == 0;
}
public int size() {
return n;
}
private void resize(int capacity) {
String[] copy = new String[capacity];
for (int i = 0; i < n; ++i) {
copy[i] = queue[(first + i) % queue.length];
}
queue = copy;
first = 0;
last = n;
}
public void enqueue(String s) {
if (n == queue.length) {
resize(2 * queue.length);
}
queue[last++] = s;
if (last == queue.length) {
last = 0;
}
n++;
}
public String dequeue() {
if(isEmpty()) {
throw new NoSuchElementException("Underflow");
}
String s = queue[first];
queue[first] = null;
first++;
n--;
if (first == queue.length) {
first = 0;
}
if (n > 0 && n == queue.length / 4) {
resize(queue.length / 2);
}
return s;
}
public static void main(String[] args) {
String[] string = {"a", "b", "c", "d", "e"};
CircularQueue queue = new CircularQueue();
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
for (String s : string) {
queue.enqueue(s);
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
while (!queue.isEmpty()) {
System.out.println("Deuque : " + queue.dequeue());
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
System.out.println("Size : " + queue.size() + ", isEmpty: " + queue.isEmpty());
}
}