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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
| #include <conio.h>
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
struct List_O//первый список
{
int Data;
List_O* next;
};
void Print_O(List_O* begin)
{
if (begin != NULL)
{
cout << begin->Data << "\t";
Print_O(begin->next);
}
else cout << "\n";
}
void Insert_O(List_O* begin, unsigned n, int val)
{
unsigned i = 1;
List_O*tmp = NULL;
List_O* first = NULL ;
while (i < (n - 1) && begin->next)
{
begin = begin->next;
i++;
}
tmp = (List_O*)malloc(sizeof(List_O));
tmp->Data = val;
if (begin->next)
{
tmp->next = begin->next;
//иначе на NULL
}
else tmp->next = NULL;
begin->next = tmp;
}
void Search_O(List_O* begin, int val)//поиск элемента
{
bool f = false;
int i = 1;
while (begin)
{
if (begin->Data == val)
{
f = true;
cout << "Элемент найден,его позиция: " << i << endl;
break;
}
else begin = begin->next;
i++;
}
if (f == false) cout << "Элемент не найден" << endl;
}
void Delete_O(List_O* head, int data)//удаление элемента
{
List_O* temp = head;
List_O* el = NULL;
if (head && head->Data == data)
{
el = head;
head = head->next;
delete el;
return;
}
while (temp->next)
{
if (temp->next->Data == data)
{
el = temp->next;
temp->next = el->next;
delete el;
break;
}
temp = temp->next;
}
}
void Init_O(int n, List_O** begin)//инициализация
{
int a;
if (n > 0)
{
(*begin) = new List_O();
cout << "Введите значение: ";
cin >> a;
(*begin)->Data = a;
(*begin)->next = NULL;
Init_O(n - 1, &((*begin)->next));
}
}
//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
struct node//двусвязный список
{
int value;
struct node* next;
struct node* prev;
};
struct node* head;
struct node* tail;
void init()
{
head = NULL;
tail = NULL;
}
void insertFirst(int element)
{
struct node* newItem;
newItem = new node;
if (head == NULL)
{
head = newItem;
newItem->prev = NULL;
newItem->value = element;
newItem->next = NULL;
tail = newItem;
}
else
{
newItem->next = head;
newItem->value = element;
newItem->prev = NULL;
head->prev = newItem;
head = newItem;
}
}
void insertLast(int element)
{
struct node* newItem;
newItem = new node;
newItem->value = element;
if (head == NULL)
{
head = newItem;
newItem->prev = NULL;
newItem->next = NULL;
tail = newItem;
}
else
{
newItem->prev = tail;
tail->next = newItem;
newItem->next = NULL;
tail = newItem;
}
}
void insertAfter(int old, int element)
{
struct node* newItem;
newItem = new node;
struct node* temp;
temp = head;
if (head == NULL)
{
cout << "could not insert" << endl;
return;
}
if (head == tail)
{
if (head->value != old)
{
cout << "could not insert" << endl;
return;
}
newItem->value = element;
head->next = newItem;
newItem->next = NULL;
head->prev = NULL;
newItem->prev = head;
tail = newItem;
return;
}
if (tail->value == element)
{
newItem->next = NULL;
newItem->prev = tail;
tail->next = newItem;
tail = newItem;
return;
}
while (temp->value != old)
{
temp = temp->next;
if (temp == NULL)
{
cout << "Could not insert" << endl;
cout << "element not found" << endl;
return;
}
}
newItem->next = temp->next;
newItem->prev = temp;
newItem->value = element;
temp->next->prev = newItem;
temp->next = newItem;
}
void deleteItem(int element)
{
struct node* temp;
temp = head;
if (head == tail)
{
if (head->value != element)
{
cout << "could not delete" << endl;
return;
}
head = NULL;
tail = NULL;
delete temp;
return;
}
if (head->value == element)
{
head = head->next;
head->prev = NULL;
delete temp;
return;
}
else if (tail->value == element)
{
temp = tail;
tail = tail->prev;
tail->next = NULL;
delete temp;
return;
}
while (temp->value != element)
{
temp = temp->next;
if (temp == NULL)
{
cout << "element not found" << endl;
return;
}
}
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
delete temp;
}
struct node* searchItem(int element)
{
struct node* temp;
temp = head;
while (temp != NULL)
{
if (temp->value == element)
{
return temp;
break;
}
temp = temp->next;
}
return NULL;
}
void printList()
{
struct node* temp;
temp = head;
while (temp != NULL)
{
printf("%d->", temp->value);
temp = temp->next;
}
puts("");
}
//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
struct key
{
int x;
key* next;
};
//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
void menu()
{
cout << "Выберите пункт из списка:" << endl;
cout << "1. Распечатать список.(однонаправлен. список)" << endl;
cout << "2.Добавить элемент в начало, середину, конец или другое указаное место списка.(однонаправлен. список)" << endl;
cout << "3.Удалить элемент из указаного места.(однонаправлен. список)" << endl;
cout << "4.Поиск элемента в списке.(однонаправлен. список)" << endl;
cout << "5.Добавить в начало.(двунапр.список)" << endl;
cout << "6.Добавить в конец.(двунапр.список)" << endl;
cout << "7.Добавить после указаного элемента.(двунапр.список)" << endl;
cout << "8.Поиск элемента в списке.(двунапр.список)" << endl;
cout << "9.Распечатать список.(двунапр.список)" << endl;
cout << "10.Удалить элемент из списка.(двунапр.список)" << endl;
cout << "Если хотите выйти нажмите у" << endl;
}
int main()
{
init();
List_O* begin = NULL; int n = 0;
int pos, val, udal, sear, num;
char exit;
setlocale(LC_ALL, "rus");
cout << "Введите размер n (n>1): ";
cin >> n;
Init_O(n, &begin);
cout << "=================================================================" << endl;
do {
menu();//case 1-4= однонаправленный список
int choice;
cin >> choice;
switch (choice) {
case 1:
{
cout << "Исходный список: ";
Print_O(begin);
}break;
case 2:
{
if (begin != NULL)
{
cout << "Введите позицию (pos>0), после которой хотите вставить новый элемент: ";
cin >> pos;
cout << "Введите вставляемое число: ";
cin >> val;
Insert_O(begin, pos, val);
cout << "Исходный список: ";
Print_O(begin);
}
}break;
case 3:
{
if (begin != NULL)
{
cout << "Введите удаляемый элемент: ";
cin >> udal;
Delete_O(begin, udal);
cout << "Исходный список: ";
Print_O(begin);
}
}break;
case 4:
{
cout << "Введите искомое число: ";
cin >> sear;
Search_O(begin, sear);
}break;
case 5:
{
int element;
cout << "Введите число";
cin >> element;
insertFirst(element);
printList();
}break;
case 6:
{
int element;
cout << "Введите число";
cin >> element;
insertLast(element);
printList();
}break;
case 7:
{
int old, newitem;
cout << "Введите после какого числа вы хотите ввести новое";
cin >> old;
cout << "Новое число";
cin >> newitem;
insertAfter(old, newitem);
printList();
}break;
case 8:
{
int item;
cout << "Введите число, которое хотите найти";
cin >> item;
struct node* ans = searchItem(item);
if (ans != NULL) cout << "Найдено " << ans->value << endl;
else cout << "Не найдено" << endl;
}break;
case 9:
{
printList();
}break;
case 10:
{
int element;
cout << "Введите число для удаления" << endl;
cin >> element;
deleteItem(element);
printList();
}break;
case 11:
{
}break;
}
cout << "Хотите выйти?" << endl;
cin >> exit;
} while (exit != 'y');
system("pause");
return 0;
} |