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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct list_t
{
int value;
struct list_t* next;
} TList;
//-----------------------------------------------------------------------------
TList* Push(TList** list, int value)
{
TList* node = (TList*) malloc(sizeof(TList));
node->value = value;
node->next = *list;
*list = node;
return *list;
}
//-----------------------------------------------------------------------------
void Clear(TList** list)
{
TList* node;
while (*list)
{
node = *list;
*list = (*list)->next;
free(node);
}
}
//-----------------------------------------------------------------------------
TList* Find(TList* list, int value)
{
for (; list && (list->value != value); list = list->next) { ; }
return list;
}
//-----------------------------------------------------------------------------
void Print(TList* list)
{
for (; list; list = list->next)
{
printf("%d ", list->value);
}
printf("\n");
}
//-----------------------------------------------------------------------------
int Random(int min, int max)
{
return (rand() % (max - min) + min);
}
//-----------------------------------------------------------------------------
TList* GetGenList(size_t count, int min, int max)
{
TList* list = NULL;
while (count--)
{
Push(&list, Random(min, max));
}
return list;
}
//-----------------------------------------------------------------------------
TList* GetUniqueFirst(TList* first, TList* second)
{
TList* list = NULL;
for (; first; first = first->next)
{
if (!Find(list, first->value) && !Find(second, first->value))
{
Push(&list, first->value);
}
}
return list;
}
//-----------------------------------------------------------------------------
int main()
{
srand(time(NULL));
TList* l1 = GetGenList(10, 3, 10);
TList* l2 = GetGenList(10, 0, 7);
TList* l = GetUniqueFirst(l1, l2);
printf("L1: "); Print(l1);
printf("L2: "); Print(l2);
printf("L : "); Print(l);
Clear(&l);
Clear(&l2);
Clear(&l1);
system("pause");
return 0;
} |