1 / 1 / 0
Регистрация: 17.04.2010
Сообщений: 55
|
|
1
|
В двусвязном списке продублировать те элементы, которые меньше следующего, но больше больше предыдущего.
12.12.2010, 11:45. Показов 974. Ответов 0
C++ | 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 "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
struct LIST {
int info;
LIST *next;
LIST *prev;
};
LIST *head;
int n1, n2;
void Initial()
{
head = new LIST;
head->next=head;
head->prev=head;
}
int Empty()
{
if (head->next==head && head->prev==head)
return 1;
else
return 0;
}
LIST *SearchOnward(int num)
{
LIST *tmp=head->next;
while(tmp!=head && tmp->info!=num)
{
tmp=tmp->next;
n1+=1;
}
if (tmp!=head)
return tmp;
else
return NULL;
}
LIST *SearchBack(int num)
{
LIST *tmp=head->prev;
while(tmp!=head && tmp->info!=num)
{
tmp=tmp->prev;
n2+=1;
}
if (tmp!=head)
return tmp;
else
return NULL;
}
int Del(int num)
{
LIST *current=SearchOnward(num);
if (current!=NULL)
{
current->prev->next=current->next;
current->next->prev=current->prev;
delete current;
return 1;
}
return 0;
}
int AddAfter(int num, int point)
{
if (Empty()==1)
{
LIST *tmp=new LIST;
tmp->next=head;
tmp->prev=head;
tmp->info=num;
head->next=tmp;
head->prev=tmp;
return 1;
}
LIST *current=SearchOnward(point);
if (current!=NULL)
{
LIST *tmp=new LIST;
tmp->next=current->next;
tmp->prev=current;
tmp->info=num;
current->next->prev=tmp;
current->next=tmp;
return 1;
}
return 0;
}
int AddBefore(int num, int point)
{
LIST *current=SearchBack(point);
if(current!=NULL)
{
LIST *tmp=new LIST;
tmp->info=num;
tmp->next=current;
tmp->prev=current->prev;
current->prev->next=tmp;
current->prev=tmp;
return 1;
}
return 0;
}
void ShowOnward()
{
LIST *tmp=head->next;
cout << endl << "В прямом направлении:" << endl;
while(tmp!=head)
{
cout << tmp->info << " ";
tmp=tmp->next;
}
cout << endl;
}
void ShowBack()
{
LIST *tmp=head->prev;
cout << endl << "В обратном направлении:" << endl;
while(tmp!=head)
{
cout << tmp->info << " ";
tmp=tmp->prev;
}
cout << endl;
}
int Double()
{
}
int main()
{
setlocale(LC_ALL,"Russian");
Initial();
int num, tmp;
char otv, otv2;
do
{
cout << "1. Добавление" << endl
<< "2. Удаление" << endl
<< "3. Вывод"<< endl
<< "4. Поиск" << endl
<< "5. Дублирование" << endl
<< "0. Выход" << endl
<< " = ";
cin >> otv;
switch(otv)
{
case '1':
cout << endl << "Введите элемент = ";
cin >> num;
if (Empty()==1)
{
AddAfter(num,0);
cout << endl << "Элемент добавлен" << endl;
}
else
{
cout << endl << "1. Добавить перед"
<< endl << "2. Добавить после"
<< endl << " = ";
cin >> otv2;
switch(otv2)
{
case '1':
cout << endl << "Перед каким элементом добавить = ";
cin >> tmp;
if (AddBefore(num,tmp)==1)
cout << endl << "Элемент добавлен" << endl;
else
cout << endl << "Такого элемента не существует" << endl;
break;
case '2':
cout << endl << "После какого элемента добавить = ";
cin >> tmp;
if (AddAfter(num,tmp)==1)
cout << endl << "Элемент добавлен" << endl;
else
cout << endl << "Такого элемента не существует" << endl;
break;
default:
cout << endl << "Ошибка" << endl;
break;
}
}
break;
case '2':
if(Empty()==1)
cout << endl << "Список пуст" << endl;
else
{
cout << endl << "Удаляемый элемент = ";
cin >> num;
if(Del(num)==1)
cout << endl << "Элемент удален" << endl;
else
cout << endl << "Такого элемента не существует" << endl;
}
break;
case '3':
if(Empty()==1)
cout << endl << "Список пуст" << endl;
else
{
cout << endl << "1. Обход в прямом напрвлении" << endl
<< "2. Обход в обратном направлении" << endl
<< " = ";
cin >> otv2;
switch(otv2)
{
case '1':
ShowOnward();
break;
case '2':
ShowBack();
break;
default:
cout << endl << "Ошибка" << endl;
break;
}
}
break;
case '4':
if(Empty()==1)
cout << endl << "Список пуст" << endl;
else
{
cout << endl << "Элемент для поиска = ";
cin >> num;
n1=0;
n2=0;
if(SearchOnward(num)!=NULL && SearchBack(num)!=NULL)
{
cout << endl << "Элемент найден" << endl;
cout << endl << "В прямом направлении шаг = " << n1 << endl;
cout << endl << "В обратном направлении шаг = " << n2 << endl;
}
else
cout << endl << "Такого элемента не существует" << endl;
}
break;
case '5':
if(Empty()==1)
cout << endl << "Список пуст" << endl;
else
{
//Double();
//ShowOnward();
}
break;
case '0':
break;
default:
cout << endl << "Ошибка" << endl;
break;
}
}while(otv!='0');
cin.get();
} |
|
В функции Double() хотела сделать это самое дублирование, но столкнулась с проблемой сравнения элементов и копирования со сдвигом элементов в списке. Можете объяснить как это сделать?
0
|