include iostream include conio include string using namespace std stru

  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
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
struct element{
string data;
int uniq_id;
element *next;
element *prev;
};
class list_cl{
public:
element *listHead;
element *listEnd;
int countElem;
list_cl(){
listHead = NULL;
listEnd = NULL;
countElem = 0;
}
~list_cl(){
listDelete();
}
void addToList(string data){
element *list = new element();
if(listHead == NULL){
list->prev = NULL;
list->next = NULL;
listHead = list;
list->uniq_id = 1;
}else{
listEnd->next = list;
list->next = NULL;
list->uniq_id = listEnd->uniq_id + 1;
}
list->data = data;
list->next = NULL;
list->prev = listEnd;
listEnd = list;
countElem++;
}
void printList(int print_method){
element *list = listHead;
if(listHead == NULL){
cout << "ERROR: spisok pystoi" << endl;
}else{
if(print_method == 1){
while(list != NULL){
cout << list->data << "[" << list->uniq_id << "], ";
list = list->next;
}
}else if(print_method == 2){
element *list = listEnd;
while(list != NULL){
cout << list->data << "[" << list->uniq_id << "], ";
list = list->prev;
}
}
}
}
void listDelete(){
element *list = listHead;
while(list != NULL){
list = list->next;
delete list;
}
listHead = NULL;
listEnd = NULL;
countElem = 0;
}
void listElementDelete(int elemNum){
element *list = listHead;
//if(elemNum > countElem){
// cout << "Element ne naiden!" << endl;
//}else{
if(countElem == 1 and elemNum == listHead->uniq_id){
element *list = listHead;
delete list;
listHead = NULL;
listEnd = NULL;
countElem--;
}else{
//if(elemNum != listEnd->uniq_id){
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id ;
}
//}else{
//list = listEnd;
//}
if(list->prev == NULL){
if(countElem == 1){
listHead = NULL;
listEnd = NULL;
}else{
list->next->prev = NULL;
listHead = list->next;
}
delete list;
countElem--;
}
if(list->next == NULL){
if(countElem == 1){
listHead = NULL;
listEnd = NULL;
}else{
list->prev->next = NULL;
listEnd = list->prev;
}
delete list;
countElem--;
}
if(list->prev != NULL && list->next != NULL){
list->prev->next = list->next;
list->next->prev = list->prev;
delete list;
countElem--;
}
//}
}
}
bool checkElement(int elemNum){
element *list = listHead;
//if(listHead->uniq_id == elemNum){cout << "111" << endl;return true;
//}
for(int i=listHead->uniq_id; i<=elemNum;){
if(list->uniq_id == elemNum){
return true;
}else{
return false;;
}
list = list->next;
i=list->uniq_id ;
}
}
};
int main(){
restart:
string elem;
int action, print_method, elemNum;
bool stop_add = false;
list_cl *list = new list_cl();
cout << "Vvodite elementu spiska: " << endl << endl;
while(stop_add == false){
cout << endl << "Dobavit' novui element:" << " ";
cin >> elem;
if(elem != "stop"){
list->addToList(elem);
}else{
stop_add = true;
}
}
if(stop_add = true){
cout << endl << "==========================" << endl << endl;
choice:
cout << "Viberite deystvie: " << endl;
cout << "1 - Napechatat' pervui element spiska" << endl;
cout << "2 - Napechatat' poslednui element spiska" << endl;
cout << "3 - Dobavit' novui element v spisok" << endl;
cout << "4 - Nauti dliny spiska" << endl;
cout << "5 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "6 - Ydalit' spisok" << endl;
cout << "7 - Ydalit' odin element spiska" << endl;
cout << "8 - Novui spisok" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
while(action != 0){
switch(action){
case 1:{ cout << "Pervui element spiska: " << list->listHead->data; break; }
case 2:{ cout << "Poslednui element spiska: " << list->listEnd->data; break; }
case 3:{
cout << endl << "Dobavit' novui element:" << " ";
cin >> elem;
list->addToList(elem);
break;
}
case 4:{ cout << "Dlina spiska: " << list->countElem; break; }
case 5: {
cout << "Poryadok vuvoda: 1 - pryamom poryadok; 2 - obratnui poryadok" << "; variant #";
cin >> print_method;
if(list->countElem > 0){
if(print_method == 1){
cout << "Vubran poryadok #1 - vuvod na ekran s na4ala: ";
list->printList(1);
}else if(print_method == 2){
cout << "Vubran poryadok #2 - vuvod na ekran s konca: ";
list->printList(2);
}else{
cout << "ERROR" << endl;
}
}
break;
}
case 6:{
list->listDelete();
cout << "Spisok yspeshno ydalen!";
cout << endl << endl << "==========================" << endl << endl;
goto restart;
break;
}
case 7:{
cout << "Ykajite nomer elementa spiska dlya ydaleniya: ";cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
list->listElementDelete(elemNum);
cout << "Element #" << elemNum << " yspeshno ydalen!";
if(list->countElem > 0){
cout << endl << "Novui spisok: "; list->printList(1);
}else if(list->countElem == 0){
cout << endl << endl << "Spisok bul polnostui ochishen!" << endl <<"==========================" << endl << endl;
goto restart;
}
}
break;
}
case 8:{ list->listDelete(); goto restart; break; }
default:{
cout << "Takogo varianta ne sywestvyet! Vuberite sna4ala" << endl << endl;
goto choice;
}
}
cout << endl << endl << "==========================" << endl << endl;
cout << "Viberite deystvie: " << endl;
cout << "1 - Napechatat' pervui element spiska" << endl;
cout << "2 - Napechatat' poslednui element spiska" << endl;
cout << "3 - Dobavit' novui element v spisok" << endl;
cout << "4 - Nauti dliny spiska" << endl;
cout << "5 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "6 - Ydalit' spisok" << endl;
cout << "7 - Ydalit' odin element spiska" << endl;
cout << "8 - Novui spisok" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
}
}
delete list;
getch();
return 0;
}