This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlaba5.c
200 lines (183 loc) · 4.53 KB
/
laba5.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct node
{
int element;
struct node* next;
};
void EnterAndCheckInput(int* a)
{
while (!scanf("%d",
&(*a)))
{
printf("Неверный ввод, пожалуйста, повторите ввод :)\n");
while (getchar() != '\n');
printf("..новый ввод: ");
}
}
void MemoryCheck(struct node* pointer)
{
if (pointer == NULL)
{
printf("\nНедостаточно памяти :<");
exit(1);
}
}
struct node* AddElement(struct node* listRoot, int val)
{
struct node* current = listRoot, * newNode;
while (current->next != NULL)
{
current = current->next;
}
newNode = (struct node*)malloc(sizeof(struct node));
MemoryCheck(newNode);
newNode->element = val;
newNode->next = NULL;
current->next = newNode;
return newNode;
}
struct node* Sort(struct node* listRoot)
{
struct node* current = listRoot, * tmp = NULL, * prev = NULL;
int flag = 0;
do
{
flag = 0;
current = listRoot;
while (current->next != NULL)
{
if (current->element > current->next->element)
{
if (current == listRoot)
{
tmp = current;
current = tmp->next;
tmp->next = current->next;
current->next = tmp;
listRoot = current;
flag = 1;
}
else
{
tmp = current;
current = tmp->next;
tmp->next = current->next;
current->next = tmp;
prev->next = current;
flag = 1;
}
}
prev = current;
current = current->next;
}
} while (flag == 1);
return listRoot;
}
void DeleteDuplicateElements(struct node* listRoot)
{
struct node* current = listRoot, * placeholder;
while (current != NULL && current->next != NULL)
{
if (current->element == current->next->element)
{
placeholder = current->next->next;
free(current->next);
current->next = placeholder;
}
current = current->next;
}
}
struct node* PlusList(struct node* listPlusRoot, struct node* generalListRoot)
{
struct node* listPlus = listPlusRoot, * generalList = generalListRoot;
while (listPlus != NULL)
{
if (generalListRoot == NULL)
{
generalListRoot = (struct node*)malloc(sizeof(struct node));
MemoryCheck(generalListRoot);
generalListRoot->element = listPlus->element;
generalListRoot->next = generalList;
}
else
generalList = AddElement(generalListRoot, listPlus->element);
listPlus = listPlus->next;
}
return generalListRoot;
}
void ShowList(struct node* listRoot)
{
struct node* current;
current = listRoot;
while (current != NULL)
{
printf(" %d ", current->element);
current = current->next;
}
}
int main(int argc, char* argv[])
{
system("chcp 1251");
system("cls");
struct node* list1Root = NULL, * list2Root = NULL;
struct node* list1 = NULL, * list2 = NULL;
int i = 0;
int n1 = 0;
printf("Введите кол-во элементов для первого списка = ");
EnterAndCheckInput(&n1);
printf("Введите элемент первого списка:\n");
for (i = 0; i < n1; i++)
{
int x = 0;
scanf("%d", &x);
if (list1Root == NULL)
{
list1Root = (struct node*)malloc(sizeof(struct node));
MemoryCheck(list1Root);
list1Root->element = x;
list1Root->next = list1;
}
else
list1 = AddElement(list1Root, x);
}
int n2 = 0;
printf("\nВведите кол-во элементов для второго списка = ");
EnterAndCheckInput(&n2);
printf("Введите элемент второго списка:\n");
for (i = 0; i < n2; i++)
{
int x = 0;
scanf("%d", &x);
if (list2Root == NULL)
{
list2Root = (struct node*)malloc(sizeof(struct node));
MemoryCheck(list2Root);
list2Root->element = x;
list2Root->next = list2;
}
else
list2 = AddElement(list2Root, x);
}
system("cls");
printf("\nПервый список:\n");
list1Root = Sort(list1Root);
ShowList(list1Root);
printf("\nВторой список:\n");
list2Root = Sort(list2Root);
ShowList(list2Root);
DeleteDuplicateElements(list1Root);
DeleteDuplicateElements(list2Root);
struct node* generalListRoot = NULL, * generalList = NULL;
generalListRoot = PlusList(list1Root, generalListRoot);
generalListRoot = PlusList(list2Root, generalListRoot);
generalListRoot = Sort(generalListRoot);
DeleteDuplicateElements(generalListRoot);
printf("\nНовый список\n");
ShowList(generalListRoot);
printf("\n");
free(list1Root);
free(list2Root);
return 0;
}