-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
319 lines (276 loc) · 5.35 KB
/
main.cpp
File metadata and controls
319 lines (276 loc) · 5.35 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <ctime>
using namespace std;
struct student
{
string name;
string surname;
int index;
student *next; //pointer to the next element
student(); //constructor
};
student::student()
{
next = 0;
}
class list
{
public:
student *first; //pointer to the beginning of the list
void add_student(string name, string surname, int index);
void remove_student(int nr);
void remove_last();
void remove_student_index(int nr,int size);
void find(int nr);
int size();
void display_student();
list();
};
list::list()
{
first = 0; //constructor
}
void list::add_student(string name, string surname, int index)
{
student *nowy = new student; //create new list item
//now we will fill new item with our data
nowy->name = name;
nowy->surname = surname;
nowy->index =index;
if (first == 0) //if this is our first item
{
first = nowy; // our new item is our first item
}
else
{
student *temp = first;
while (temp->next)
{
temp = temp->next;
}
temp->next = nowy;
nowy->next = 0;
}
}
int list::size()
{
int size = 0;
student *temp = first;
while (temp)
{
temp = temp->next;
size++;
}
return size;
}
void list::remove_last()
{
student *osoba = first;
if (osoba)
{
if (osoba->next)
{
while (osoba->next->next)
osoba = osoba->next;
delete osoba->next;
osoba->next = 0;
}
else
{
delete osoba;
first = 0;
}
}
}
void list::remove_student_index(int nr,int size)
{
int j = 0;
bool remove = 1;
student *osoba = new student;
student *wskaznik = new student;
osoba = first;
if (osoba->index == nr)
{
first = osoba->next;
delete osoba;
}
else
{
while (j++ < size)
{
if (osoba->next->index == nr)
{
if (j == size) remove_last();
else
{
wskaznik = osoba->next;
osoba->next = osoba->next->next;
delete wskaznik;
remove = 0;
break;
}
}
else osoba = osoba->next;
}
if (remove) cout << "Nie znaleziono elementu" << endl;
}
}
void list::remove_student(int nr)
{
if (nr == 1) //if this is the first item
{
student *temp = first;
first = temp->next;
}
if (nr >= 2) //if this is not the first item
{
int j = 1;
student *temp = first;
while (temp)
{
if ((j + 1) == nr) break;
temp = temp->next;
j++;
}
if (temp->next->next == 0)
temp->next = 0;
else
temp->next = temp->next->next;
}
}
void list::find(int nr)
{
student *osoba = new student;
osoba = first;
if (osoba->index == nr)
{
//cout << "Znalezione" << endl;
}
else
{
bool flaga = 1;
while (osoba->next != NULL)
{
if (osoba->next->index == nr)
{
//cout << "Znalezione" << endl;
flaga = 0;
break;
}
osoba = osoba->next;
}
// if (flaga) cout << "Nie znaleziono" << endl;
}
}
/*
void list::find(int Nnr_indeksu)
{
student *osoba = new student;
osoba = glowa;
if (osoba->index == Nnr_indeksu)
{
//reutrn osoba
cout << "Osoba o indeksie: " << Nnr_indeksu << " to: " << osoba->name << " " << osoba->surname << endl;
}
else
{
bool flaga = 1;
while (osoba->next != NULL)
{
if (osoba->next->nr_indeksu == Nnr_indeksu)
{
//return osoba
cout << "Osoba o indeksie: " << Nnr_indeksu << " to: " << osoba->next->name << " " << osoba->next->surname << endl;
flaga = 0;
break;
}
osoba = osoba->next;
}
if (flaga) cout << "Nie znaleziono takiej osoby" << endl;
}
}
*/
void list::display_student()
{
student *temp = first;
while (temp)
{
cout << "imie:" << temp->name << " nazwisko:" << temp->surname << " nr:" << temp->index << endl;
temp = temp->next;
}
}
void wypelnij()
{
ifstream wejscie;
wejscie.open("dane10.txt");
ifstream wejscie2;
wejscie2.open("studenci10.txt");
ofstream wynik;
wynik.open("wypelnianieLista10.txt");
ofstream wynik2;
wynik2.open("usuwanieLista10.txt");
auto begin = std::chrono::high_resolution_clock::now();
list *baza = new list;
for (int i = 0; i < 14000; i++)
{
string name;
string surname;
int index;
wejscie >> name;
wejscie >> surname;
wejscie >> index;
baza->add_student(name, surname, index);
}
int liczba = baza->size();
auto end = std::chrono::high_resolution_clock::now();
wynik << 14000 << " " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "\n";
auto begin2 = std::chrono::high_resolution_clock::now();
for (int i = 0; i <14000; i++)
{
int indeks;
wejscie2 >> indeks;
baza->remove_student_index(indeks, liczba);
liczba = baza->size();
}
auto end2 = std::chrono::high_resolution_clock::now();
wynik2 <<14000 << " " << std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - begin2).count() << "\n";
}
void szukanie()
{
ifstream wejscie;
wejscie.open("dane10.txt");
ifstream wejscie2;
wejscie2.open("studenci10.txt");
ofstream wynik;
wynik.open("szukanieLista10.txt");
list *baza = new list;
for (int i = 0; i < 14000; i++)
{
string name;
string surname;
int index;
wejscie >> name;
wejscie >> surname;
wejscie >> index;
baza->add_student(name, surname, index);
}
auto begin = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 14000; i++)
{
int indeks;
wejscie2 >> indeks;
baza->find(indeks);
}
auto end = std::chrono::high_resolution_clock::now();
wynik << 14000 << " " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "\n";
}
int main()
{
wypelnij();
szukanie();
system("pause");
}