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
|
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void Intersection( List L1, List L2, List InterList )
{
Position P, TmpCell;
TmpCell = InterList;
for( L1 = L1->Next; L1 != NULL; L1 = L1->Next )
{
P = Find( L1->Element, L2 );
if( P )
{
TmpCell = Insert( P->Element, InterList, TmpCell);
}
}
}
/* This solution is dirty, needed to be modify */
void Union( List L1, List L2, List UniList )
{
Position P, TmpCell;
Position P1, P2;
TmpCell = UniList;
P1 = L1->Next;
P2 = L2->Next;
while( P1 && P2 )
{
if ( P1->Element == P2->Element )
{
TmpCell = Insert( P1->Element, UniList, TmpCell );
P1 = P1->Next;
P2 = P2->Next;
}
while( P2 && P1->Element > P2->Element )
{
TmpCell = Insert( P2->Element, UniList, TmpCell );
P2 = P2->Next;
}
while( P1 && P1->Element < P2->Element )
{
TmpCell = Insert( P1->Element, UniList, TmpCell );
P1 = P1->Next;
}
}
while( P1 )
{
TmpCell = Insert( P1->Element, UniList, TmpCell );
P1 = P1->Next;
}
while( P2 )
{
TmpCell = Insert( P2->Element, UniList, TmpCell );
P2 = P2->Next;
}
}
void
UnionSectionVersion( List L1, List L2, List UniList )
{
Position P, TmpCell;
TmpCell = UniList;
L1 = L1->Next;
L2 = L2->Next;
while( L1 || L2 )
{
if( !L1 )
{
TmpCell = Insert( L2->Element, UniList, TmpCell );
L2 = L2->Next;
continue;
}
if( !L2 )
{
TmpCell = Insert( L1->Element, UniList, TmpCell );
L1 = L1->Next;
continue;
}
if( L1->Element == L2->Element )
{
TmpCell = Insert( L1->Element, UniList, TmpCell );
L1 = L1->Next;
L2 = L2->Next;
continue;
}
if( L1->Element < L2->Element )
{
TmpCell = Insert( L1->Element, UniList, TmpCell );
L1 = L1->Next;
continue;
}
if( L1->Element > L2->Element)
{
TmpCell = Insert( L2->Element, UniList, TmpCell );
L2 = L2->Next;
continue;
}
}
}
int main()
{
int Arr1[10] = {7, 6, 5, 4, 3, 2, 1};
int Arr2[10] = {10, 8, 6, 4, 2};
List L1, L2, InterList, UniList;
L1 = malloc( sizeof( struct Node ) );
L2 = malloc( sizeof( struct Node ) );
InterList = malloc( sizeof( struct Node ) );
UniList = malloc( sizeof( struct Node ) );
ConstructList( L1, Arr1, 7 );
ConstructList( L2, Arr2, 5 );
Intersection( L1, L2, InterList );
UnionSectionVersion( L1, L2, UniList );
PrintList( L1 );
PrintList( L2 );
PrintList( InterList );
PrintList( UniList );
return 0;
}
|