-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoundedQueue.java
194 lines (155 loc) · 4.77 KB
/
BoundedQueue.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
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
187
188
189
190
191
192
193
194
package rsn170330.sp02;
import java.util.Scanner;
/**
* CS 5V81: Implementation of Data Structures and Algorithms
* Short Project 02 SP2: Implementation of Bounded Queue
* @author Rahul Nalawade
* @author Bharath Rudra
*
* Date: 2018-Aug-08
*/
public class BoundedQueue<T> {
private int maxSize;
private int front;
private int rear;
private T[] bQ;
/**
* Implement a bounded-sized queue BoundedQueue<T>, using arrays with the following operations:
* To avoid "generic array cannot be created" error, declare the array to be Object[] and
* type-cast where needed to avoid type warnings.
*
* BoundedQueue(int size):
* Constructor for queue of given size
*/
public BoundedQueue(int size) {
maxSize = size;
bQ = (T[]) new Object[maxSize];
front = rear = -1; // Empty Queue condition/ Initialization
//lastOperationAdded = false;
}
/**
* Add a new element x at the rear of the queue
* @param x the element to be added
* @return false if the element was not added because the queue is full
*/
boolean offer(T x) {
// When boundedQueue(bQ) is full
if (size() == maxSize) return false;
else {
if (front == -1) { front++; } // The pointers (f,r) are at (-1,-1)
rear = (rear + 1) % maxSize; // incrementing pointers in circular way
bQ[rear] = x;
}
return true;
}
/**
* Remove and return the element at the front of the queue
* @return null if the queue is empty
*/
public T poll() {
// the element to be polled (removed)
T element;
// When bQ is not empty
if (!this.isEmpty()) {
element = (T) bQ[front];
// When the last element is to be removed, resulting into empty bQ.
if (front == rear) front = rear = -1; // Empty Queue condition/ Initialization
else front = (front+1) % maxSize; // circular increment of front
return element;
}
return null;
}
/**
* return front element, without removing it (null if queue is empty)
* @return the front element
*/
public T peek() {
T element;
// When bQ is not empty
if (!this.isEmpty()) {
element = (T) bQ[front];
return element;
}
return null;
}
// return the number of elements in the queue
public int size() {
// empty bQ state of (f,r) = (-1,-1)
if (front == -1 && rear == -1) { return 0; }
// e.g. when (f,r) = (5,2): size = 10 + (2 - 5) + 1 = 8.
if (rear < front) { return maxSize + (rear - front) + 1; }
return (rear - front + 1);
}
// check if the queue is empty
public boolean isEmpty() {
// When (f,r) = (-1,-1)
if (size() == 0) return true;
return false;
}
// clear the queue (size=0)
public void clear() {
front = rear = -1; // Empty Queue condition/ Initialization
}
/**
* fill user supplied array with the elements of the queue, in queue order
* @param a the supplied array
*/
public void toArray(T[] a) {
int l = size();
for (int i=0; i<l; i++) {
// i iterating from front to rear in circular way
a[i] = (T) bQ[(front+i) % maxSize];
}
}
public static void main(String args[]) {
Integer a[];
Scanner in = new Scanner(System.in);
System.out.println("Enter maxSize of BoundedQueue: ");
int maxSize = in.nextInt();
BoundedQueue<Integer> b = new BoundedQueue<>(maxSize);
System.out.println("Enter choices 1. offer() 2. poll() 3. peek() 4. size() 5. isEmpty() 6. clear() 7. toArray(): 8. Quit");
int choice = in.nextInt();
while(choice != 8) {
switch(choice) {
case 1: // offer(x)
Integer e = (Integer) in.nextInt();
System.out.println("Element offered: "+b.offer(e));
// Checking front and rear pointers
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 2: // poll()
System.out.println("Element polled: "+b.poll());
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 3: // peek()
System.out.println("boundedQueue[front] = "+b.peek());
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 4: // size()
System.out.println("Size: "+b.size());;
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 5: // isEmpty()
System.out.println("boundedQueue isEmpty: "+b.isEmpty());
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 6: // clear()
b.clear();
System.out.println("(f,r) = "+b.front+" "+b.rear);
break;
case 7: // toArray()
a = new Integer[b.size()];
b.toArray(a);
System.out.println("(f,r) = "+b.front+" "+b.rear);
System.out.print("Array in queue order: ");
for (int z: a) {System.out.print(z+" ");}
System.out.println();
break;
case 8: // Exit
break;
}
System.out.println("Enter choices 1. offer() 2. poll() 3. peek() 4. size() 5. isEmpty() 6. clear() 7. toArray(): 8. Quit");
choice = in.nextInt();
}
}
}