Здравствуйте! Помогите пожалуйста дописать программу. Задача: создать однонаправленный список. И реализовать в нем функции (добавление в начало, конец; вставка; удаление начала, конца, всего списка; вывод списка; поиск, 3 способа сортировки любого элемента класса(car). Пирамидальная, быстрая, вставками.) Проблема в чем, программа компилируется, я заполняю элемент, но когда использую метод вывода ничего не выводит в консоль.
Использовать шаблоны нельзя. Заранее спасибо!
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
| #include <iostream>
#include <string>
using namespace std;
class Car
{
public:
string car_model;
int engine_power;
string engines_type;
int net_weight;
string car_color;
Car()
{
cout << "Введите модель автомобиля: ";
cin >> car_model;
cout << "Введите мощность двигателя: ";
cin >> engine_power;
cout << "Введите тип двигателя: ";
cin >> engines_type;
cout << "Введите массу нетто автомобиля: ";
cin >> net_weight;
cout << "Введеите цвет кузова автомобиля: ";
cin >> car_color;
}
};
class List
{
public:
Car item;
List *root;
List *next;
int number;
void add_begin(List *root, int num);
void add_end(List *root, int num);
void get(List *root);
void insert(List *root, int num);
void delete_begin(List *root);
void delete_end(List *root);
void clear(List *root);
bool search(List *root, int key);
};
void List::add_begin(List *root, int num)
{
List *temp = new List;
temp->number = num;
temp->next = root;
root = temp;
}
void List::add_end(List *root, int num)
{
List *temp = new List;
temp->number = num;
temp->next = NULL;
if (root = NULL)
{
root = temp;
}
else
{
List *current = root;
while (current != NULL)
{
current = current->next;
}
current->next = temp;
}
}
void List::get(List *root)
{
List *current = root;
while (current != NULL)
{
cout << endl;
cout << "Модель машины: " << current->item.car_model << endl;
cout << "Мощность двигателя машины: " << current->item.engine_power << endl;
cout << "Тип двигателя машины: " << current->item.engines_type << endl;
cout << "Масса нетто машины: " << current->item.net_weight << endl;
cout << "Цвет кузова машины: " << current->item.car_color << endl;
current = current->next;
}
}
void List::insert(List *root, int num)
{
List *temp = new List;
temp->number = num;
int currentindex = 0;
List *current = root;
while (current != NULL)
{
if (num == 0)
{
temp->next = root;
root = temp;
}
if (num < 0)
{
return;
}
if (currentindex == num)
{
temp->next = current->next;
current->next = temp;
}
currentindex++;
current = current->next;
}
}
void List::delete_begin(List *root)
{
List *temp = root->next;
delete root;
root = temp;
}
void List::delete_end(List *root)
{
List *current = root;
while (true)
{
if (current->next->next == NULL)
{
delete current->next;
current->next = NULL;
current = current->next;
return;
}
else current = current->next;
}
}
void List::clear(List *root)
{
List *temp = new List;
List *current = root;
while (current != NULL)
{
temp = current;
current = current->next;
delete temp;
}
root = NULL;
}
bool List::search(List *root, int key)
{
List *current = root;
while (current != NULL)
{
if (current->number == key)
{
return true;
}
else
{
return false;
}
current = current->next;
}
}
int main()
{
setlocale(0, "");
int ans, n = 0, key;
List Item;
List *root = NULL;
do
{
cout << "Введите\n1 - для добавления элемента в начало списка\n2 - для добавления элемента в конец списка\n3 - для вывода в консоль списка\n4 - для вставки элемента в нужное место\n5 - удаления первого элемента списка\n6 - для удаления последнего элемента спика\n7 - для удаления всего списка\n8 - для поиска элемента\n0 - выход из программы";
cin >> ans;
switch (ans)
{
case 1:
{
system("cls");
n++;
Item.add_begin(root, n);
break;
}
case 2:
{
system("cls");
n++;
Item.add_end(root, n);
break;
}
case 3:
{
system("cls");
Item.get(root);
break;
}
case 4:
{
system("cls");
n++;
Item.insert(root, n);
break;
}
case 5:
{
system("cls");
Item.delete_begin(root);
break;
}
case 6:
{
system("cls");
Item.delete_end(root);
break;
}
case 7:
{
system("cls");
Item.clear(root);
break;
}
case 8:
{
system("cls");
cout << "Введите номер элемента, который хотите найти: ";
cin >> key;
Item.search(root, key);
break;
}
default:
{
system("cls");
break;
}
}
} while (ans != 0);
system("pause");
return 0;
} |
|