forked from logicipher/PL0-in-USTC-of-Compiler-Theory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.c
225 lines (187 loc) · 3.39 KB
/
set.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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "set.h"
symset uniteset(symset s1, symset s2)
{
symset s;
snode* p;
s1 = s1->next;
s2 = s2->next;
s = p = (snode*) malloc(sizeof(snode));
while (s1 && s2)
{
p->next = (snode*) malloc(sizeof(snode));
p = p->next;
if (s1->elem < s2->elem)
{
p->elem = s1->elem;
s1 = s1->next;
}
else if (s1->elem > s2->elem) // modified by nanahka 17-11-20
{
p->elem = s2->elem;
s2 = s2->next;
}
else
{
p->elem = s1->elem;
s1 = s1->next;
s2 = s2->next;
}
}
while (s1)
{
p->next = (snode*) malloc(sizeof(snode));
p = p->next;
p->elem = s1->elem;
s1 = s1->next;
}
while (s2)
{
p->next = (snode*) malloc(sizeof(snode));
p = p->next;
p->elem = s2->elem;
s2 = s2->next;
}
p->next = NULL;
return s;
} // uniteset
symset uniteset_mul(symset s1, .../* NULL */) // added by nanahka 17-11-13
{
va_list list;
symset s = s1;
va_start(list, s1);
s1 = va_arg(list, symset);
while (s1)
{
s = uniteset(s, s1);
s1 = va_arg(list, symset);
}
va_end(list);
return s;
}
void setinsert(symset s, int elem)
{
snode* p = s;
snode* q;
while (p->next && p->next->elem < elem)
{
p = p->next;
}
if (p->next && p->next->elem == elem) // added by nanahka 17-11-20
{
return;
}
q = (snode*) malloc(sizeof(snode));
q->elem = elem;
q->next = p->next;
p->next = q;
} // setinsert
void setinsert_mul(symset s, .../* SYM_NULL */) // added by nanahka 17-11-20
{
va_list list;
int elem;
va_start(list, s);
elem = va_arg(list, int);
while (elem)
{
setinsert(s, elem);
elem = va_arg(list, int);
}
va_end(list);
}
symset expandset(symset s, .../* SYM_NULL */) // added by nanahka 17-11-13
{
va_list list;
symset p;
int elem;
p = (snode*) malloc(sizeof(snode));
p->next = NULL;
va_start(list, s);
elem = va_arg(list, int);
while (elem)
{
setinsert(p, elem);
elem = va_arg(list, int);
}
va_end(list);
return uniteset(s, p);
} // expandset
void deleteset(symset s, .../* SYM_NULL */) // added by nanahka 17-11-21
{
va_list list;
symset p;
int elem;
p = (snode*) malloc(sizeof(snode));
p->next = NULL;
va_start(list, s);
elem = va_arg(list, int);
while (elem)
{
setinsert(p, elem);
elem = va_arg(list, int);
}
snode *s0, *q;
s0 = s;
s = s->next;
q = p->next;
while (s && q)
{
if (s->elem == q->elem)
{
s0->next = s->next;
s->next = NULL;
destroyset(s);
s = s0->next;
q = q->next;
}
else if (s->elem > q->elem)
{
q = q->next;
}
else
{
s0 = s;
s = s->next;
}
}
destroyset(p);
}
symset createset(int elem, .../* SYM_NULL */)
{
va_list list;
symset s;
s = (snode*) malloc(sizeof(snode));
s->next = NULL;
va_start(list, elem);
while (elem)
{
setinsert(s, elem);
elem = va_arg(list, int);
}
va_end(list);
return s;
} // createset
void destroyset(symset s)
{
snode* p;
while (s)
{
p = s;
p->elem = -1000000;
s = s->next;
free(p);
}
} // destroyset
int inset(int elem, symset s)
{
s = s->next;
while (s && s->elem < elem)
s = s->next;
if (s && s->elem == elem)
return 1;
else
return 0;
} // inset
// EOF set.c