-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskStackQueue.java
187 lines (172 loc) · 6.16 KB
/
TaskStackQueue.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
///////////////////
// TaskStackQueue: A queue OR stack (depending on constructor value) consisting of objects of type Task; contains all typical queue/stack functions
///////////////////
public class TaskStackQueue extends OS {
private String classType;
private int numAllowed = 150;
public int numCreated = 0;
public Task[] dataArray = new Task[numAllowed];
//TaskStackQueue (constructor) - requires that the data structure type is named to proceed
TaskStackQueue(String inputClassType) {
if (inputClassType.equals("queue") || inputClassType.equals("stack")) {
classType = inputClassType;
} else {
console.error("Invalid TaskStackQueue constructor value.");
}
}
//add - enqueue or push, depending on the structure type
public boolean add(Task newTask) {
if (classType.equals("queue")) {
if (enqueue(newTask)) {
return true;
}
} else if (classType.equals("stack")) {
if (push(newTask)) {
return true;
}
}
return false;
}
//enqueue - takes the input task and adds it to the queue
public boolean enqueue(Task newTask) {
verifyDatatype("queue");
if (!isFull()) {
dataArray[numCreated] = new Task();
dataArray[numCreated].code = newTask.code;
dataArray[numCreated].description = newTask.description;
dataArray[numCreated].numCycles = newTask.numCycles;
dataArray[numCreated].length = newTask.length;
numCreated++;
return true;
} else {
console.error("Ran out of queue space - increase numAllowed");
}
//should never reach this return
return false;
}
//push - takes the input task and adds it to the stack
public boolean push(Task newTask) {
verifyDatatype("stack");
if (!isFull()) {
dataArray[numCreated] = new Task();
dataArray[numCreated].code = newTask.code;
dataArray[numCreated].description = newTask.description;
dataArray[numCreated].numCycles = newTask.numCycles;
dataArray[numCreated].length = newTask.length;
numCreated++;
return true;
} else {
console.error("Ran out of stack space - increase numAllowed");
}
//should never reach this return
return false;
}
//remove - dequeue or pop, depending on the structure type
public Task remove() {
if (classType.equals("queue")) {
return dequeue();
} else if (classType.equals("stack")) {
return pop();
} else {
Task nullTask = new Task();
return nullTask;
}
}
//dequeue - takes the task at the front of the queue and returns it
public Task dequeue() {
verifyDatatype("queue");
if (!isEmpty()) {
Task returnTask = dataArray[0];
for (int i = 1; i < numCreated; i++) {
dataArray[i - 1] = dataArray[i];
}
if(isFull()) {
console.error("Ran out of queue space - increase numAllowed");
}
dataArray[numCreated] = null;
numCreated--;
return returnTask;
} else {
Task nullTask = new Task();
return nullTask;
}
}
//pop - takes the task at the top of the stack and returns it
public Task pop() {
verifyDatatype("stack");
if (!isEmpty()) {
Task returnTask = dataArray[numCreated - 1];
if(isFull()) {
console.error("Ran out of stack space - increase numAllowed");
}
dataArray[numCreated - 1] = null;
numCreated--;
return returnTask;
} else {
Task nullTask = new Task();
return nullTask;
}
}
//peek - returns the task at the front of the queue but doesn't remove it
public Task peek() {
if (!isEmpty()) {
return dataArray[0];
} else {
Task nullTask = new Task();
return nullTask;
}
}
//contains - returns true if the queue contains the task
public boolean contains(Task taskCriteria) {
for (int i = 0; i < numCreated; i++) {
if (taskCriteria.code == dataArray[i].code && taskCriteria.numCycles == dataArray[i].numCycles && taskCriteria.description.equals(dataArray[i].description)) {
return true;
}
}
return false;
}
//print - outputs the contents of the task queue and the data inside it
public void print() {
console.log(classType + " printout:");
for (int i = 0; i < numCreated; i++) {
int j = i + 1;
char code = dataArray[i].code;
String description = dataArray[i].description;
int numCycles = dataArray[i].numCycles;
int length = dataArray[i].length;
console.log("Task " + j + ": " + code + " - " + description + " - " + numCycles + " - " + length);
}
}
//isFull - returns whether or not the queue is full
public boolean isFull() {
if (numCreated >= numAllowed) {
return true;
}
return false;
}
//isEmpty - returns whether or not the queue is empty
public boolean isEmpty() {
if (numCreated == 0) {
return true;
}
return false;
}
//verifyDatatype - used in this class' functions that require stack/queue-specific actions
private void verifyDatatype(String requiredDataType) {
if (!requiredDataType.equals(classType)) {
if (requiredDataType.equals("stack")) {
console.error("Trying to execute queue function in stack");
} else if (requiredDataType.equals("queue")) {
console.error("Trying to execute stack function in queue");
}
}
}
//erase - deletes all data
public void erase() {
numCreated = 0;
for (int i = 0; i < dataArray.length; i++) {
numCreated = 0;
dataArray[i] = new Task();
}
}
}