-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.cpp
346 lines (338 loc) · 7.65 KB
/
Code.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*Hadassah Yanofsky
ECE264 - Data Structures and Algorithms 1
Cooper Union
Fall 2019
This code uses an absract classs called SimpleList to implements stacks and queues in a singly linked list*/
#include<iostream>
#include<fstream>
#include<string>
#include<list>
using namespace std;
//parent class that holds a pointer to the first and last of the list as well as the name of the list
template <class T>
class SimpleList {
//This stores the value and pointer to the next node for each node of the list
class node {
public:
T value;
node* next;
};
protected:
node* head, * tail;
string name;
public:
//constructor
SimpleList() {
head = NULL;
tail = NULL;
}
string get_name() {
return name;
}
virtual void push(T x) = 0;
virtual T pop() = 0;
//removes top node from the list and return the value stored there
T remove_top() {
if(head != NULL) {
T value;
node* temp = new node;
temp = head;
head = head->next;
value = temp->value;
delete temp;
return value;
}
}
//adds a node to the end of the list with a value given
void add_end(T x) {
node* temp = new node;
temp->value = x;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
//adds a node to the start of the list with a value given
void add_start(T x) {
node* temp = new node;
temp->value = x;
temp->next = head;
head = temp;
}
//checks if the list is empty
bool check_list() {
if (head == NULL) {
return false;
}
return true;
}
};
//child class of SimpleList, this is just for Queue kind of lists. Last in is first out.
template<class T>
class Queue : public SimpleList<T> {
public:
//constructor
Queue(string x) {
this->head = NULL;
this->tail = NULL;
this->name = x;
}
//inserts node at the end of the list
void push(T x) {
this->add_end(x);
}
//displays and then deletes node from the front of the list
T pop() {
return this->remove_top();
}
};
//child class of SimpleList, holds just Stack kind of list. First in is first out.
template <class T>
class Stack : public SimpleList<T> {
public:
//constructor
Stack(string x) {
this->head = NULL;
this->tail = NULL;
this->name = x;
}
//adds node to the front of the list.
void push(T x) {
this->add_start(x);
}
//displays and then removes node from the front of the list.
T pop() {
return this->remove_top();
}
};
//Class that holds functions that read and print from files.
class file {
string in, out;
//list of all the diffrent kind of lists made, both Queues and Stacks.
list<SimpleList<int>*>listSLi;
list<SimpleList<double>*>listSLd;
list<SimpleList<string>*>listSLs;
//files to read and write to
ifstream infile;
ofstream outfile;
public:
bool check_end() {
if (infile.eof()) {
return true;
}
return false;
}
//Prompt user to say which input and output file
void getname() {
cout << "Enter name of input file: ";
cin >> in;
cout << "Enter name of output file: ";
cin >> out;
}
//open files
void open_files() {
infile.open(in);
outfile.open(out);
}
//constructor
file() {
getname();
open_files();
}
//destructor
~file() {
infile.close();
outfile.close();
}
//read the commands and then call function accordingly
void read() {
string command, name;
int y = 0;
infile >> command;
infile >> name;
if (name.substr(0, 1) == "i") {
y = 1;
}
else if (name.substr(0, 1) == "d") {
y = 2;
}
else {
y = 3;
}
if (command == "create") {
create(y, name);
}
else if (command == "push") {
push(y, name);
}
else if (command == "pop") {
pop(y, name);
}
}
//Search function
template <typename T>
SimpleList<T>* Search(list<SimpleList<T>*>& vartypeList, string listName) {
for (typename list<SimpleList<T>*>::iterator it = vartypeList.begin(); it != vartypeList.end(); ++it) {
if (((*it)->get_name()) == listName) {
return *it;
}
}
return nullptr;
}
//checks if list already exists and then if it doesn't it creates a new list
void create(int y, string name) {
string type;
infile >> type;
outfile << "PROCESSING COMMAND: create " << name << " " << type << "\n";
if (y == 1) {
SimpleList<int>* temp = Search(listSLi, name);
if (temp!=nullptr) {
outfile << "ERROR: This name already exists!\n";
}
else {
SimpleList<int>* pSLi;
if (type == "queue") {
pSLi = new Queue<int>(name);
}
else {
pSLi = new Stack<int>(name);
}
listSLi.push_front(pSLi);
}
}
else if (y == 2) {
SimpleList<double>* temp = Search(listSLd, name);
if (temp != nullptr) {
outfile << "ERROR: This name already exists!\n";
}
else {
SimpleList<double>* pSLd;
if (type == "queue") {
pSLd = new Queue<double>(name);
}
else {
pSLd = new Stack<double>(name);
}
listSLd.push_front(pSLd);
}
}
else {
SimpleList<string>* temp = Search(listSLs, name);
if (temp != nullptr) {
outfile << "ERROR: This name already exists!\n";
}
else {
SimpleList<string>* pSLs;
if (type == "queue") {
pSLs = new Queue<string>(name);
}
else {
pSLs = new Stack<string>(name);
}
listSLs.push_front(pSLs);
}
}
}
//finds the list and the calls list's push to add new node with value
void push(int y, string name) {
outfile << "PROCESSING COMMAND: push " << name << " ";
if (y == 1) {
int value;
infile >> value;
outfile << value << "\n";
SimpleList<int>* temp= Search(listSLi, name);;
if (temp == nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
temp->push(value);
}
}
else if (y == 2) {
double value;
infile >> value;
outfile << value << "\n";
SimpleList<double>* temp = Search(listSLd, name);;
if (temp == nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
temp->push(value);
}
}
else if (y == 3) {
string value;
infile >> value;
outfile << value << "\n";
SimpleList<string>* temp = Search(listSLs, name);
if (temp == nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
temp->push(value);
}
}
}
//finds list and then calls list's pop to remove node
void pop(int y, string name) {
outfile << "PROCESSING COMMAND: pop " << name << "\n";
if (y == 1) {
SimpleList<int>* temp = Search(listSLi, name);
if (temp == nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
if (temp->check_list()) {
outfile << "Value popped: " << temp->pop() << "\n";
}
else {
outfile << "ERROR: This list is empty!\n";
}
}
}
else if (y == 2) {
SimpleList<double>* temp = Search(listSLd, name);
if (temp==nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
if (temp->check_list()) {
outfile << "Value popped: " << temp->pop() << "\n";
}
else {
outfile << "ERROR: This list is empty!\n";
}
}
}
else if (y == 3) {
SimpleList<string>* temp = Search(listSLs, name);;
if (temp==nullptr) {
outfile << "ERROR: This name does not exist!\n";
}
else {
if (temp->check_list()) {
outfile << "Value popped: " << temp->pop() << "\n";
}
else {
outfile << "ERROR: This list is empty!\n";
}
}
}
}
};
int main() {
file first;
//reads until end of the file
while (!first.check_end()) {
first.read();
}
return 1;
}