-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacks_queues.cpp
319 lines (288 loc) · 10.1 KB
/
stacks_queues.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Justin Koe. stacks_queues program that parses an input text file containing commands creating / reading from stacks and queues of int, double, and string, returns an output text file with output of the input commands.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <list>
#include <iterator>
template <typename T>
class SimpleList {
private:
struct Node {
T data;
Node *next;
Node( const T &d, Node *n = NULL ) : data( d ), next( n ) {}
};
Node *start;
Node *end;
std::string name;
protected:
void push_front( const T &val );
void push_back( const T &val );
T pop_front();
public:
SimpleList(std::string name) {
start = NULL;
end = NULL;
this->name = name;
}
std::string get_name() {
return this->name;
}
bool check_empty();
virtual void push(const T &val) = 0;
virtual T pop() = 0;
};
template <typename T>
void SimpleList<T>::push_front( const T &val ) {
Node *front = new Node(val);
Node *old = this->start;
if(this -> start == NULL) { // empty
this -> start = front;
this -> end = front;
}
else {
this -> start = front;
front -> next = old;
}
}
template <typename T>
void SimpleList<T>::push_back( const T &val ) {
Node *back = new Node(val);
if(this -> start == NULL) { // empty
this -> end = back;
this -> start = back;
}
else {
this -> end -> next = back;
this -> end = back;
}
}
template <typename T>
T SimpleList<T>::pop_front() {
Node *front = this->start;
Node *new_front = front->next;
if (this->start == NULL) {
this->end = NULL;
return 0;
}
else {
T ret = front->data;
this->start = new_front;
delete front;
return ret;
}
}
template <typename T>
bool SimpleList<T>::check_empty() {
if(this->start == NULL) {
return true;
}
return false;
}
template <typename T>
class Stack : public SimpleList<T> {
public:
Stack(std::string name) : SimpleList<T>(name) {};
void push(const T &val);
T pop();
};
template <typename T>
void Stack<T>::push(const T &val) {
this->push_front(val);
}
template <typename T>
T Stack<T>::pop() {
return this->pop_front();
}
template <typename T>
class Queue : public SimpleList<T> {
public:
Queue(std::string name) : SimpleList<T>(name) {};
void push(const T &val);
T pop();
};
template <typename T>
void Queue<T>::push(const T &val) {
this->push_back(val);
}
template <typename T>
T Queue<T>::pop() {
return this->pop_front();
}
// SEARCH FUNCTION: templated search inside of list of SimpleList<> for a specified name
template <typename T>
SimpleList<T>* name_search(std::list<SimpleList<T> *> sl_list, const std::string &name) {
typename std::list<SimpleList<T> *>::iterator itr;
for(itr = sl_list.begin(); itr != sl_list.end(); itr++) {
if((*itr) -> get_name() == name) {
return (*itr);
}
}
return NULL;
}
void process_file(const std::string &file_in, const std::string &file_out) {
std::list<SimpleList<int> *> listSLi; // all integer stacks and queues
std::list<SimpleList<double> *> listSLd; // all double stacks and queues
std::list<SimpleList<std::string> *> listSLs; // all string stacks and queues
SimpleList<int> *pSLi;
SimpleList<double> *pSLd;
SimpleList<std::string> *pSLs;
std::string line, s_val;
int i_val;
double d_val;
std::string command, name, type;
std::ifstream ifile (file_in);
std::ofstream ofile (file_out);
if (ifile.is_open()) {
while(std::getline (ifile, line)) {
std::stringstream ss;
ss << line;
ss >> command >> name;
if (command.compare("create") == 0) {
ss >> type;
ofile << "PROCESSING COMMAND: " << command << " " << name << " " << type << std::endl;
if (name.at(0) == 'i') {
if (name_search(listSLi, name) != NULL) {
ofile << "ERROR: This name already exists!" << std::endl;
}
else {
if (type.compare("stack") == 0) {
pSLi = new Stack<int>(name);
listSLi.push_front(pSLi);
}
else {
pSLi = new Queue<int>(name);
listSLi.push_front(pSLi);
}
}
}
else if (name.at(0) == 'd') {
if (name_search(listSLd, name) != NULL) {
ofile << "ERROR: This name already exists!" << std::endl;
}
else {
if (type.compare("stack") == 0) {
pSLd = new Stack<double>(name);
listSLd.push_front(pSLd);
}
else {
pSLd = new Queue<double>(name);
listSLd.push_front(pSLd);
}
}
}
else {
if (name_search(listSLs, name) != NULL) {
ofile << "ERROR: This name already exists!" << std::endl;
}
else {
if (type.compare("stack") == 0) {
pSLs = new Stack<std::string>(name);
listSLs.push_front(pSLs);
}
else {
pSLs = new Queue<std::string>(name);
listSLs.push_front(pSLs);
}
}
}
}
else if (command.compare("push") == 0) {
if (name.at(0) == 'i') {
ss >> i_val;
ofile << "PROCESSING COMMAND: " << command << " " << name << " " << i_val << std::endl;
pSLi = name_search(listSLi, name);
if (pSLi == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
pSLi->push(i_val);
}
}
else if (name.at(0) == 'd') {
ss >> d_val;
ofile << "PROCESSING COMMAND: " << command << " " << name << " " << d_val << std::endl;
pSLd = name_search(listSLd, name);
if(pSLd == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
pSLd->push(d_val);
}
}
else {
ss >> s_val;
ofile << "PROCESSING COMMAND: " << command << " " << name << " " << s_val << std::endl;
pSLs = name_search(listSLs, name);
if(pSLs == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
pSLs->push(s_val);
}
}
}
else { // pop
ofile << "PROCESSING COMMAND: " << command << " " << name << std::endl;
if (name.at(0) == 'i') {
pSLi = name_search(listSLi, name);
if (pSLi == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
if(pSLi->check_empty() == true) {
ofile << "ERROR: This list is empty!" << std::endl;
}
else {
i_val = pSLi->pop();
ofile << "Value popped: " << i_val << std::endl;
}
}
}
else if (name.at(0) == 'd') {
pSLd = name_search(listSLd, name);
if (pSLd == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
if(pSLd->check_empty() == true) {
ofile << "ERROR: This list is empty!" << std::endl;
}
else {
d_val = pSLd->pop();
ofile << "Value popped: " << d_val << std::endl;
}
}
}
else {
pSLs = name_search(listSLs, name);
if (pSLs == NULL) {
ofile << "ERROR: This name does not exist!" << std::endl;
}
else {
if(pSLs->check_empty() == true) {
ofile << "ERROR: This list is empty!" << std::endl;
}
else {
s_val = pSLs->pop();
ofile << "Value popped: " << s_val << std::endl;
}
}
}
}
}
}
ifile.close();
ofile.close();
}
int main() {
std::string file_in, file_out;
std::cout << "Enter name of input file: ";
std::cin >> file_in;
std::cout << "Enter name of output file: ";
std::cin >> file_out;
process_file(file_in, file_out);
}