-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (137 loc) · 4.39 KB
/
main.cpp
File metadata and controls
225 lines (137 loc) · 4.39 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
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
ifstream in_data;
ofstream out_data;
class node {
friend class list;
public:
int data;
node* next;
node() {
data = -9999;
next = nullptr;
}
node(int incoming) {
this->data = incoming;
next = nullptr;
}
};
class list {
int rejects=0;
node *head;
node *tail;
public:
list() {
node* n = new node();
head = n;
tail = n;
}
void append(int x) {
node* newnode;
newnode = new node(x);
if(head->next == NULL) head->next = newnode;
tail->next = newnode;
tail = newnode;
}
int getRejects() {
return rejects;
}
node* Spot(int findInt) {
node* temp = head;
if(temp->next==nullptr) {
out_data<<"\nList Empty!\n";
return NULL;
}
while(temp->next!=nullptr && temp->next->data<findInt) {
temp=temp->next;
}
return temp;
}
void display() {
node* temp = head->next;
if(temp==nullptr) {
out_data<<"\nList Empty!\n";
return;
}
while(temp!=nullptr) {
out_data<<temp->data<<" ";
temp = temp->next;
}
out_data<<"\n\n";
}
void altPrint() {
node*temp = head;
if(temp==nullptr) {
out_data<<"\nList Empty!\n";
return;
}
out_data<<"listHead";
while(temp->next!=nullptr) {
out_data<<" --> ("<<temp->data<<", "<<temp->next->data<<")";
if(temp->next->next==nullptr) {
out_data<<" --> ("<<temp->next->data<<","<<" -1)";
}
temp = temp->next;
}
}
void insert(int inData) {
//if list is currently empty, we append the incoming element and return
if (head->next==nullptr) {
this->append(inData);
out_data<<"\nInserting "<<inData<<": ";
altPrint();
return;
}
node* newnode = new node(inData);
node* temp = this->Spot(inData); //use spot to find info and save to temp node
//if list already has at least one element inside and duplicate data found, reject duplicate and return
if(temp->next!=nullptr && temp->next->data==inData) {
out_data<<"\n\n!Rejecting attempt to insert duplicate: "<<inData<<"\n";
rejects++;
return;
}
//otherwise insert element within sorted list
out_data<<"\nInserting "<<inData<<": ";
newnode->next = temp->next;
temp->next = newnode;
altPrint();
}
};
int main(int argc, char * argv[]) {
if(argc<3) {
cout<<"Insufficient Arguments, argv[1] is input txt file name, argv[2] is output txt file name \n\n";
return 0;
}
out_data.open(argv[2]);
out_data<<"Elements within input file: ";
in_data.open(argv[1]);
int count=0;
int num;
if (in_data.is_open()) {
while (in_data>>num) {
count++;
out_data<<num<<" ";
}
out_data<<"\n\n"<<"Total number of elements found in file: "<<count<<"\n";
in_data.close();
}
list newlist; //CREATE LINKED LIST OBJECT 'newlist'
in_data.open(argv[1]);
if (in_data.is_open()) {
while (in_data>>num) {
newlist.insert(num);
}
out_data<<"\n"<<newlist.getRejects()<< " duplicates were rejected from list";
in_data.close();
}
out_data<<"\n\n";
out_data<<"Final numbers remaining in list: ";
newlist.display();
out_data<<"\n";
out_data.close();
cout<<"Please refer to output file specified in argv[2]\n\n";
return 0;
};