-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (186 loc) · 4.52 KB
/
main.cpp
File metadata and controls
195 lines (186 loc) · 4.52 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
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <limits>
#include <iterator>
#include <string>
#include <cstring>
#include <ctype.h>
#include <vector>
#include "ItemType.h"
#include "ListNode.h"
#include "SortedLinkedList.h"
using namespace std;
//Creates the list from an input file "input.txt"
SortedLinkedList createList() {
ifstream sFile("input.txt");
vector<int> vals;
int num;
ItemType item;
SortedLinkedList created = SortedLinkedList();
if (!sFile.is_open()) {
cout << "File not found" << endl;
}
while (sFile >> num) {
vals.push_back(num);
}
for(const auto &i: vals) {
item.initialize(i);
created.insertItem(item);
}
sFile.close();
return created;
} // create
int main(int argc, char *argv[]) {
// Check that program is started properly with input.txt
if (argc != 2) {
cout << "Usage ./main input.txt || ./main empty.txt" << endl;
return 1;
} else if (strcmp(argv[1], "input.txt") != 0) {
if (strcmp(argv[1], "empty.txt") != 0) {
cout << "Usage ./main input.txt || ./main empty.txt" << endl;
return 1;
}
}
// create the linked list
SortedLinkedList list = SortedLinkedList();
if (strcmp(argv[1], "input.txt") == 0) {
list = createList();
}
char choice;
int val;
string input, temp;
ItemType toInsert, toDel, toFind, next, mergeNode;
cout << "Commands:" << endl;
cout << "(i) - Insert value" << endl;
cout << "(d) - Delete value" << endl;
cout << "(s) - Search value" << endl;
cout << "(n) - Print next iterator value" << endl;
cout << "(r) - Reset iterator" << endl;
cout << "(a) - Delete alternate nodes" << endl;
cout << "(m) - Merge two lists" << endl;
cout << "(t) - Intersection" << endl;
cout << "(p) - Print list" << endl;
cout << "(l) - Print length" << endl;
cout << "(q) - Quit program" << endl;
while(1) {
cout << "Enter a command: ";
cin >> (choice);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch(choice) {
case 'i':
if (list.length() != 0) {
list.printList();
}
cout << "Enter number: ";
while(true) {
cin >> val;
if(!cin) {
cout << "Please enter an integer" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
} else break;
}
toInsert.initialize(val);
list.insertItem(toInsert);
list.printList();
break;
case 'd':
list.printList();
cout << "Enter value to delete: ";
cin >> val;
toDel.initialize(val);
list.deleteItem(toDel);
list.printList();
break;
case 's':
cout << "Enter a value to search: ";
cin >> val;
toFind.initialize(val);
if (list.searchItem(toFind) > -1) {
cout << "Index " << list.searchItem(toFind) << endl;
} else {
cout << "Item not found." << endl;
}
break;
case 'n':
next = list.getNextItem();
if (next.getValue() != -1) {
cout << next.getValue() << endl;
}
break;
case 'r':
list.resetList();
cout << "Iterator Reset" << endl;
break;
case 'a':
cout << "List before alternate delete: ";
list.printList();
list.deleteAlternatingNodes();
cout << "List after alternate delete: ";
list.printList();
break;
case 'm': {
SortedLinkedList toMerge, merged;
int mergeSize;
int num;
cout << "Length of list to merge: ";
cin >> mergeSize;
cout << "List elements separated by spaces in order: ";
while (mergeSize-- > 0) {
cin >> num;
mergeNode.initialize(num);
toMerge.insertItem(mergeNode);
}
cout << "List 1: ";
list.printList();
cout << "List 2: ";
toMerge.printList();
merged = list.merge(list, toMerge);
merged.printList();
list = merged;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
case 't': {
SortedLinkedList intersection, intersector;
ItemType intersectionNode;
cout << "Length of list to find intersection: ";
int comListSize;
int num;
cin >> comListSize;
cout << "List elements separated by spaces in order: ";
while (comListSize-- > 0) {
cin>>num;
intersectionNode.initialize(num);
intersector.insertItem(intersectionNode);
}
cout << "List 1: ";
list.printList();
cout << "List 2: ";
intersector.printList();
cout << "Intersection:";
list.findCommon(intersector);
cout << endl;
//list.printList();
break;
}
case 'p':
list.printList();
break;
case 'l':
cout << "List length is " << list.length() << endl;
break;
case 'q':
cout << "Quitting program..." << endl;
return 0;
default:
cout << "Invalid command, try again!" << endl;
break;
}
}
return 0;
} //main