-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCircularQ.java
More file actions
128 lines (106 loc) · 3.65 KB
/
ArrayCircularQ.java
File metadata and controls
128 lines (106 loc) · 3.65 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
// Course Code: CSC 211-651
// Assignment: 3 - Queue
// Due Date: 10/13/2020
// Author:Christopher Yonek
/* Description: This creates a queue that items go into and the functions in the program allow elements to be shifted, added and removed
*/
import java.util.Arrays;
public class ArrayCircularQ<T> implements QInterface<T>{
/*Initializes an array to hold the queue in,
*qRear adds an new item to the queue while elements are removed from qFront.
*qContents is the current number of items in the queue, qMax is the most
*a queue can hold. The default size is 5.
*/
private T[] qArry;
private int qFront;
private int qRear;
private int qContents;
private int qMax;
private final int DEFAULT_SIZE = 5;
/* This is a no-arg constructor that just assigns the
*default value, instead of always needing an argument in
*ArrayCircularQ()
*/
public ArrayCircularQ() {
qMax = DEFAULT_SIZE;
qArry = (T[]) new Object[qMax];
qFront = 0;
qRear = -1;
qContents = 0;
}
/*A constructor assigning initial values,
*the front initially has zero while the back has one less.
*It also starts as an empty Queue, TestArrayCircularQ
*uses this method
*/
public ArrayCircularQ(int s) {
qMax = s;
T[] tempQArry = (T[]) new Object[qMax];
qFront = 0;
qRear = -1;
qContents = 0;
qArry = tempQArry;
}
/*When qRear is evenly divisible by qMax
*the modulo of qRear + 1 on qMax is zero.
*this allows you to wrap entries around the queue.
*/
public void insert(T newEntry) {
qRear = (qRear + 1) % qMax;
qArry[qRear] = newEntry;
qContents++;
}
/*This removes an element in the front of the Queue.
*It doesn't shift any elements in the Array holding the queue
*but puts an empty string in place of the removed element.
*/
public T remove(){
if(qContents == 0) //if the queue is empty give null
return null;
T qCount = qArry[qFront];
qArry[qFront] = null;
qFront = (qFront + 1) % qMax;
if(qFront == qMax) //This reset the front to zero so
qFront = 0; // qFront can't be greater than qMax
qContents--; //since item is removed, the contents decreases
return qCount;
}
/* Returns the front of the Queue */
public T peek() {
return qArry[qFront];
}
/*Checks if the queue is empty*/
public boolean isEmpty() {
return (qContents == 0);
}
/* Removes everything in the queue for null */
public void clear(){
qFront = 0;
qRear = -1;
qContents = 0;
}
/*This prints out the contents of
*the Queue and the array associated with it.
*It will tell the user if its empty.
*/
public void printQ() {
if(qContents < 1){
System.out.println("The queue is empty");
return;
}
System.out.println("Circular Array: "+Arrays.toString(qArry));
System.out.print("Circular Array Queue: ");
int tempQfront = qFront;
for(int i = 1; i <= qContents; i++) {
System.out.print(qArry[tempQfront] + " "); // This for-loop prints out each
tempQfront = (tempQfront + 1) % qMax; // element in the Queue
}
System.out.println();
}
/*If the queue exceeds max amount
* option 4 in TestArrayCircular will
* return true */
public boolean isFull() {
return (qContents == qMax);
}
}