-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
265 lines (213 loc) · 5.76 KB
/
list.h
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
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
typedef struct Node {
string value;
struct Node* next;
struct Node* prev;
} node;
typedef struct List {
size_t size;
node* head;
node* tail;
List(size_t size = 0, Node* bn = nullptr) {
this->size = size;
this->head = bn;
this->tail = bn;
}
size_t getSize() {
return this->size;
}
void deleteList() {
};
void cleanList() {
while (this->size != 0) {
popBack();
}
};
void printList() {
node* tmp = this->head;
for (size_t i = 0; i < this->size; i++) {
cout << i << ": " << tmp->value << endl;
tmp = tmp->next;
}
cout << endl;
}
void pushFront(string value) {
node* tmp = new node;
tmp->value = value;
tmp->next = this->head;
tmp->prev = nullptr;
if (this->head) {
this->head->prev = tmp;
}
this->head = tmp;
if (this->tail == NULL) {
this->tail = tmp;
}
this->size++;
cout << "Element: \"" << value << "\" has been pushed to the begining." << endl;
};
void popFront() {
node* prev = this->head;
this->head = this->head->next;
if (this->head) {
this->head->prev = NULL;
}
if (prev == this->tail) {
this->tail = NULL;
}
free(prev);
this->size--;
};
void pushBack(string value, bool out = true) {
node* tmp = new node;
tmp->value = value;
tmp->next = nullptr;
tmp->prev = this->tail;
if (this->tail) {
this->tail->next = tmp;
}
this->tail = tmp;
if (this->head == NULL) {
this->head = tmp;
}
this->size++;
if (out) {
cout << "Element: \"" << value << "\" has been pushed to the end." << endl;
}
};
void popBack() {
node* next = this->tail;
this->tail = this->tail->prev;
if (this->tail) {
this->tail->next = NULL;
}
if (next == this->head) {
this->head = NULL;
}
free(next);
this->size--;
};
Node* get(size_t index) {
Node* tmp = NULL;
size_t i;
if (index < this->size / 2) {
i = 0;
tmp = this->head;
while (tmp && i < index) {
tmp = tmp->next;
i++;
}
}
else {
i = this->size - 1;
tmp = this->tail;
while (tmp && i > index) {
tmp = tmp->prev;
i--;
}
}
return tmp;
}
string read(size_t i) {
return get(i)->value;
}
size_t search(string value) {
Node* tmp = this->head;
for (size_t i = 0; i < this->size; i++) {
tmp = tmp->next;
if (tmp->value == value) {
return i;
}
}
cout << "Element wasn`t found." << endl;
}
void insert(size_t index, string value) {
if (index == -1) {
pushFront(value);
}
else if (index == size + 1) {
pushBack(value);
}
else {
Node* elm = NULL;
Node* ins = NULL;
elm = get(index);
ins = new node;
ins->value = value;
ins->prev = elm;
ins->next = elm->next;
if (elm->next) {
elm->next->prev = ins;
}
elm->next = ins;
if (!elm->prev) {
this->head = elm;
}
if (!elm->next) {
this->tail = elm;
}
this->size++;
cout << "Element: \"" << value << "\" was inserted in " << index << " position." << endl;
}
}
void deleteExactly(size_t index) {
Node* elm = NULL;
string tmp = NULL;
elm = get(index);
if (elm->prev) {
elm->prev->next = elm->next;
}
if (elm->next) {
elm->next->prev = elm->prev;
}
tmp = elm->value;
if (!elm->prev) {
this->head = elm->next;
}
if (!elm->next) {
this->tail = elm->prev;
}
free(elm);
this->size--;
cout << "Element: \"" << tmp << "\" from " << index << "position." << endl;
}
void writeFile(const char* path) {
ofstream file;
file.open(path);
if (!file.is_open()) {
cout << "Error: File can`t be open." << endl;
return;
}
node* tmp = this->head;
for (size_t i = 0; i < this->size; i++) {
file << i << ": " << tmp->value << endl;
tmp = tmp->next;
}
file.close();
}
void readFile(const char* path) {
ifstream file;
file.open(path);
if (!file.is_open()) {
cout << "Error: File can`t be open." << endl;
return;
}
char ch;
string s_tmp;
file.seekg(3, ios_base::cur);
while (file.get(ch)) {
if (ch == '\n') {
this->pushBack(s_tmp, false);
s_tmp = "";
file.seekg(3, ios_base::cur);
continue;
}
s_tmp += ch;
}
file.close();
cout << "Database was read succesfully." << endl;
}
};