Repository navigation
Expand file tree
/
Copy pathQueue.java
More file actions
186 lines (164 loc) · 5.25 KB
/
Copy pathQueue.java
File metadata and controls
186 lines (164 loc) · 5.25 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cs6301.g32;
import cs6301.g00.Timer;
import java.util.Iterator;
/**
* @author prady
* Ver 1.0: 9/10/17
*/
public class Queue<T> implements Iterable<T> {
private T[] queue;
private static final int DEFAULT_CAPACITY = 16;
private int size;
private int head, tail;
public int getSize() {
return size;
}
public int getCapacity() {
return queue.length;
}
public boolean isEmpty() {
return size == 0;
}
@SuppressWarnings("unchecked")
Queue() {
this.queue = (T[]) new Object[DEFAULT_CAPACITY];
this.size = 0;
this.head = 0;
this.tail = 0;
}
@SuppressWarnings("unchecked")
Queue(int capacity) {
assert capacity >= DEFAULT_CAPACITY;
this.queue = (T[]) new Object[capacity];
this.size = 0;
this.head = 0;
this.tail = 0;
}
/**
* This method adds an element to the queue. If the queue size is over 90% then this methods doubles the capacity of the queue
*
* @param item : Element to be added to the queue
* @return : true if the element was added successfully
*/
public boolean offer(T item) {
queue[tail] = item;
tail = (tail + 1) % queue.length;
if (++size > 0.9 * queue.length) {
resize(queue.length * 2);
}
return true;
}
/**
* This method removes and returns element at the head of the queue. If the queue is less than 25% occupied
* then it reduces the size of the queue
* only if the new size is not less than DEFAULT_CAPACITY
*
* @return Element at the head of the queue or null if the queue is empty
*/
public T poll() {
if (size == 0) return null;
T item = queue[head];
queue[head] = null;
head = head + 1;
size = size - 1;
if ((size < (0.25 * queue.length)) && ((queue.length / 2) >= DEFAULT_CAPACITY)) {
resize(queue.length / 2);
}
return item;
}
/**
* This method returns element at the head of the queue
*
* @return Element at the head of the queue or null if the queue is empty
*/
public T peek() {
return queue[head];
}
/**
* This method resizes the array to the specified capacity
*
* @param capacity : int : the new capacity of the array
*/
@SuppressWarnings("unchecked")
private void resize(int capacity) {
assert capacity >= DEFAULT_CAPACITY;
T[] tmp = (T[]) new Object[capacity];
Iterator it = this.iterator();
int i = 0;
while (true) {
tmp[i++] = (T) it.next();
if (!it.hasNext()) break;
}
queue = tmp;
head = 0;
tail = size - 1;
}
@Override
public Iterator<T> iterator() {
return new QueueIterator<>();
}
class QueueIterator<T> implements Iterator<T> {
private int cursor; // maintains state of the iterator
private int counter;
QueueIterator() {
cursor = head;
counter = 0;
}
@Override
public boolean hasNext() {
return counter < size;
}
@Override
@SuppressWarnings("unchecked")
public T next() {
T item = (T) queue[cursor];
cursor = (cursor + 1) % queue.length;
counter = counter + 1;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
@Override
public String toString() {
if (size == 0) {
return null;
}
Iterator<T> it = this.iterator();
StringBuilder sb = new StringBuilder();
while (true) {
sb.append(it.next() + " ");
if (!it.hasNext()) return sb.toString().trim();
}
}
public static void main(String[] args) {
Queue<Integer> integerQueue = new Queue<>();
System.out.println("Queue created with capacity : " + integerQueue.getCapacity());
for (int i = 1; i <= 14; i++) {
integerQueue.offer(i);
}
Timer t = new Timer();
System.out.println("Elements in queue:");
System.out.println(integerQueue);
System.out.println("Size of queue: " + integerQueue.size);
System.out.println("Total capacity of queue: " + integerQueue.getCapacity());
System.out.println("-------------Queue resize-------------");
integerQueue.offer(15);
System.out.println("Elements in queue:");
System.out.println(integerQueue);
System.out.println("Size of queue: " + integerQueue.size);
System.out.println("Total capacity of queue: " + integerQueue.getCapacity());
System.out.println("-------------Queue shrink-------------");
for(int i = 1; i<=8; i++){
System.out.println("Removed " + integerQueue.poll());
}
System.out.println("Element at top: " + integerQueue.peek());
System.out.println("Elements in queue:");
System.out.println(integerQueue);
System.out.println("Size of queue: " + integerQueue.size);
System.out.println("Total capacity of queue: " + integerQueue.getCapacity());
System.out.println(t.end());
}
}