質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -81,3 +81,99 @@
|
|
81
81
|
|
82
82
|
|
83
83
|
ご教示願います。
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
*追記*
|
88
|
+
|
89
|
+
```C
|
90
|
+
|
91
|
+
#include <stdio.h>
|
92
|
+
|
93
|
+
#include <stdlib.h>
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
struct node {
|
98
|
+
|
99
|
+
int elm;
|
100
|
+
|
101
|
+
struct node *next;
|
102
|
+
|
103
|
+
};
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
struct node *new(int e, struct node *n) {
|
108
|
+
|
109
|
+
struct node *p;
|
110
|
+
|
111
|
+
p = malloc(sizeof(struct node));
|
112
|
+
|
113
|
+
p->elm = e;
|
114
|
+
|
115
|
+
p->next = n;
|
116
|
+
|
117
|
+
return p;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
struct node *diff(struct node *a, struct node *b) {
|
124
|
+
|
125
|
+
struct node *head = new(0, NULL), *tail = head;
|
126
|
+
|
127
|
+
if(a==NULL || b== NULL) {
|
128
|
+
|
129
|
+
return a;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
if(a->elm < b->elm) {
|
134
|
+
|
135
|
+
tail->next = new(a->elm, NULL);
|
136
|
+
|
137
|
+
tail= tail->next;
|
138
|
+
|
139
|
+
return tail->next = diff(a->next, b);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
else if(a->elm == b->elm) {
|
144
|
+
|
145
|
+
return tail->next = diff(a->next, b->next);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
else {
|
150
|
+
|
151
|
+
return tail->next = diff(a, b->next);
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
int main(void) {
|
160
|
+
|
161
|
+
struct node *a = new(1, new(3, new(6, new(10, NULL))));
|
162
|
+
|
163
|
+
struct node *b = new(3, new(7, NULL));
|
164
|
+
|
165
|
+
struct node *c = diff(a,b);
|
166
|
+
|
167
|
+
while(c!=NULL) {
|
168
|
+
|
169
|
+
printf("%d\n",c->elm);
|
170
|
+
|
171
|
+
c = c->next;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
return 0;
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
```
|
1
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
![イメージ説明](96
|
1
|
+
![イメージ説明](2189435e683b9067953c278f8ac95284.jpeg)
|
2
|
-
|
3
|
-
|
4
2
|
|
5
3
|
上の問題において
|
6
4
|
|
@@ -14,4 +12,72 @@
|
|
14
12
|
|
15
13
|
であってますでしょうか?
|
16
14
|
|
15
|
+
|
16
|
+
|
17
|
+
(2)は
|
18
|
+
|
19
|
+
![イメージ説明](8191e3af30f14be1e57058fba60cb0de.jpeg)
|
20
|
+
|
21
|
+
でいいでしょうか?
|
22
|
+
|
23
|
+
各変数が保持する線形リストを図示せよと記載されているのですが、どこからリストを描けばいいか理解できませんでした。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
(3)最悪の場合は差集合A-B=A、つまり、A∩B=0であることから、(ア)の文とb=b->nextの文を繰り返すことから
|
28
|
+
|
29
|
+
O(|A|+|B|)と考えました
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
(4)
|
34
|
+
|
35
|
+
```C
|
36
|
+
|
37
|
+
struct node *diff(struct node *a, struct node *b) {
|
38
|
+
|
39
|
+
struct node *head = new(0, NULL), *tail = head;
|
40
|
+
|
41
|
+
if(a==NULL || b== NULL) {
|
42
|
+
|
43
|
+
tail->next = a;
|
44
|
+
|
45
|
+
return head->next
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
if(a->elm < b->elm) {
|
50
|
+
|
51
|
+
return diff(a->next, b);
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
else if(a->elm == b->elm) {
|
56
|
+
|
57
|
+
return diff(a->next, b->next);
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
else {
|
62
|
+
|
63
|
+
return diff(a, b->next);
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
と考えましたが、冒頭の
|
72
|
+
|
73
|
+
struct node *head = new(0, NULL), *tail = head;
|
74
|
+
|
75
|
+
が邪魔でうまく再起できません。
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
(5)(6)はできませんでした。
|
80
|
+
|
81
|
+
|
82
|
+
|
17
83
|
ご教示願います。
|