-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularArrayQueue.java
More file actions
154 lines (142 loc) · 3.9 KB
/
CircularArrayQueue.java
File metadata and controls
154 lines (142 loc) · 3.9 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
/**
* This class represents a Queue implementation using a circular array as the underlying data structure.
* @author Zicong Qu
*/
public class CircularArrayQueue<T> implements QueueADT<T> {
private int front, rear, count;
private T[] queue;
private int DEFAULT_CAPACITY = 20;
/**
* Constructor initialize front, rear, count, and capacity of the queue array.
*/
public CircularArrayQueue () {
front = 1;
rear = DEFAULT_CAPACITY;
count = 0;
queue = (T[])(new Object[DEFAULT_CAPACITY]);
}
/**
* Same as the first constructor described above, except that this one takes in
* an integer parameter for the initial capacity rather than using the default capacity.
*
* @param size the size of the queue array
*/
public CircularArrayQueue (int size) {
front = 1;
rear = size;
count = 0;
queue = (T[])(new Object[size]);
}
/** Takes in an element of the generic type and adds that element to the rear of the queue
*
* @param element the element needs to be pushed to to the rear of the queue.
*/
public void enqueue(T element) {
if (count == queue.length) {
expandCapacity();
}
rear = (rear + 1) % queue.length;
queue[rear] = element;
count ++;
}
/** Throw EmptyCollectionException if the array is empty. Remove and return the item at the front of the stack.
*
* @return the item at the font of the stack
*/
public T dequeue() throws EmptyCollectionException{
if (isEmpty()) {
throw new EmptyCollectionException("queue");
}
T element = queue[front];
front = (front + 1) % queue.length;
count --;
return element;
}
/** Throws an EmptyCollectionException if the queue is empty.
* Otherwise return the item at the front of the queue without removing it
*
* @return the item at the font of the stack
*/
public T first() throws EmptyCollectionException{
if (isEmpty()) {
throw new EmptyCollectionException("queue");
}
T element = queue[front];
return element;
}
/** Returns true if the queue is empty, and false otherwise.
*
* @return True or false indicating if the queue is empty
*/
public boolean isEmpty() {
if (count == 0) {
return true;
}else {
return false;
}
}
/** Returns the number of items on the queue.
*
* @return the number of items on the queue.
*/
public int size() {
return count;
}
/** Returns the front index value
*
* @return the front index value
*/
public int getFront() {
return front;
}
/** Returns the rear index value
*
* @return the rear index value
*/
public int getRear() {
return rear;
}
/** Returns the current length (capacity) of the array
*
* @return the current length (capacity) of the array
*/
public int getLength() {
return queue.length;
}
/** Returns the string containing "QUEUE: " followed by each of the queue's items
* in order from front to rear with ", " between each of the items and a period at the end of the last item.
* If the queue is empty then print "The queue is empty" instead.
*
* @return the string representation of the queue
*/
public String toString() {
String result;
if (isEmpty()) {
result = "The queue is empty";
}else {
result = "QUEUE: ";
for (int i = 1; i <= count; i ++) {
result += queue[i];
if (i != count) {
result += ", ";
}else {
result += ".";
}
}
}
return result;
}
/** Create a new array that has 20 more slots than the current array has.
*/
private void expandCapacity() {
int new_size = getLength() + 20;
T[] expanded_queue = (T[])(new Object[new_size]);
for (int i = front; i <= count; i ++) {
expanded_queue[i] = queue[front];
front = (front + 1) % queue.length;
}
queue = expanded_queue;
front = 1;
rear = count;
}
}