回答編集履歴

1

追記

2018/01/28 06:43

投稿

TaroToyotomi
TaroToyotomi

スコア1430

test CHANGED
@@ -3,3 +3,321 @@
3
3
  ifの条件式が真になる場合は図のようになりますが、elseの場合はどのようなケースが考えられますか?
4
4
 
5
5
  ![イメージ説明](76fc520ebbbafca1219b1b8f254894b9.png)
6
+
7
+
8
+
9
+ [追記]
10
+
11
+ 提示されたexchange()関数を実行してみました。
12
+
13
+ 仕様が不明なので何とも言えないのですが、正常に動作しているようには見えません。
14
+
15
+ exchange()関数実行後にリンクリストが壊れているように見えますが・・・。
16
+
17
+ 正常動作はどのように確認しましたか?
18
+
19
+
20
+
21
+ ```c
22
+
23
+ #include <stdio.h>
24
+
25
+ #include <stdlib.h>
26
+
27
+ #include <assert.h>
28
+
29
+
30
+
31
+ #define N 256
32
+
33
+ #define FILENAME "address.csv"
34
+
35
+
36
+
37
+ struct address{
38
+
39
+ char name[N];
40
+
41
+ char address[N];
42
+
43
+ char tel[N]; // 電話番号
44
+
45
+ char mail[N];
46
+
47
+ struct address *next;
48
+
49
+ struct address *before;
50
+
51
+ };
52
+
53
+
54
+
55
+ void exchange(struct address **p, struct address **q)
56
+
57
+ {
58
+
59
+ struct address *r, *s, *t;
60
+
61
+ assert(*p != 0 && *q != 0);
62
+
63
+
64
+
65
+ if (p == q)
66
+
67
+ return;
68
+
69
+
70
+
71
+ r = *p;
72
+
73
+ s = *q;
74
+
75
+ if (&((*p)->next) == q || &((*q)->next) == p) {
76
+
77
+ if (s->next != 0){
78
+
79
+ s->next->before = r;
80
+
81
+ }
82
+
83
+ r->before = s;
84
+
85
+ s->before = r->before;
86
+
87
+ *p = s;
88
+
89
+ *q = s->next;
90
+
91
+ s->next = r;
92
+
93
+ return;
94
+
95
+ } else {
96
+
97
+ if (s->next != 0){
98
+
99
+ s->next->before = r;
100
+
101
+ }
102
+
103
+ t = s->before;
104
+
105
+ s->before = r->before;
106
+
107
+ if (r->next != 0){
108
+
109
+ r->next->before = s;
110
+
111
+ }
112
+
113
+ r->before = t;
114
+
115
+ t = r->next;
116
+
117
+ r->next = s->next;
118
+
119
+ s->next = t;
120
+
121
+ *p = s;
122
+
123
+ *q = r;
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ void data_create( struct address **head )
132
+
133
+ {
134
+
135
+ int i;
136
+
137
+ struct address data[8] = {
138
+
139
+ { "yamada","tone","090-1122","mail-9", NULL, NULL },
140
+
141
+ { "hosi","nagoya","090-1122","mail-9", NULL, NULL },
142
+
143
+ { "kato","kanagawa","090-1122","mail-9", NULL, NULL },
144
+
145
+ { "koko","yosida","090-1122","mail-9", NULL, NULL },
146
+
147
+ { "naka","kamikosaka","090-1122","mail-9", NULL, NULL },
148
+
149
+ { "nakada","nogata","090-1122","mail-9", NULL, NULL },
150
+
151
+ { "saito","yamanashi","090-1122","mail-9", NULL, NULL },
152
+
153
+ { "suzuki","saitama","090-1122","mail-9", NULL, NULL }
154
+
155
+ };
156
+
157
+ struct address *p[9] = {NULL};
158
+
159
+
160
+
161
+ for ( i = 0; i < 8; i++ ) {
162
+
163
+ p[i] = malloc( sizeof(struct address));
164
+
165
+ *p[i] = data[i];
166
+
167
+ }
168
+
169
+
170
+
171
+ for ( i = 0; i < 8; i++ ){
172
+
173
+ if ( p[i] != NULL ) {
174
+
175
+ p[i]->next = p[i+1];
176
+
177
+ if ( i > 0 ) {
178
+
179
+ p[i]->before = p[i-1];
180
+
181
+ }
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ *head = p[0];
190
+
191
+ }
192
+
193
+
194
+
195
+ int main()
196
+
197
+ {
198
+
199
+ struct address *head = NULL;
200
+
201
+ struct address *p = NULL;
202
+
203
+ struct address *q = NULL;
204
+
205
+ data_create( &head );
206
+
207
+
208
+
209
+ printf( "オリジナルデータ表示\n");
210
+
211
+ p = head;
212
+
213
+ while ( p != NULL ) {
214
+
215
+ printf("addr=%08x, next=%08x, before=%08x, %s\n", p, p->next, p->before, p->name );
216
+
217
+ p = p->next;
218
+
219
+ }
220
+
221
+ printf("###################\n");
222
+
223
+ printf( "exchange() 実行前\n");
224
+
225
+ p = head->next;
226
+
227
+ q = head->next->next;
228
+
229
+ printf("p: addr=%08x, next=%08x, before=%08x, %s\n", p, p->next, p->before, p->name );
230
+
231
+ printf("q: addr=%08x, next=%08x, before=%08x, %s\n", q, q->next, q->before, q->name );
232
+
233
+ exchange( &p, &q );
234
+
235
+ printf( "exchange() 実行後\n");
236
+
237
+ printf("p: addr=%08x, next=%08x, before=%08x, %s\n", p, p->next, p->before, p->name );
238
+
239
+ printf("q: addr=%08x, next=%08x, before=%08x, %s\n", q, q->next, q->before, q->name );
240
+
241
+
242
+
243
+ printf("###################\n");
244
+
245
+ printf( "変更後データ表示\n");
246
+
247
+ p = head;
248
+
249
+ while ( p != NULL ) {
250
+
251
+ printf("addr=%08x, next=%08x, before=%08x, %s\n", p, p->next, p->before, p->name );
252
+
253
+ p = p->next;
254
+
255
+ }
256
+
257
+
258
+
259
+
260
+
261
+ return 0;
262
+
263
+ }
264
+
265
+ ```
266
+
267
+
268
+
269
+ 実行結果
270
+
271
+ $gcc -o main *.c
272
+
273
+ $main
274
+
275
+ オリジナルデータ表示
276
+
277
+ addr=01675010, next=01675430, before=00000000, yamada
278
+
279
+ addr=01675430, next=01675850, before=01675010, hosi
280
+
281
+ addr=01675850, next=01675c70, before=01675430, kato
282
+
283
+ addr=01675c70, next=01676090, before=01675850, koko
284
+
285
+ addr=01676090, next=016764b0, before=01675c70, naka
286
+
287
+ addr=016764b0, next=016768d0, before=01676090, nakada
288
+
289
+ addr=016768d0, next=01676cf0, before=016764b0, saito
290
+
291
+ addr=01676cf0, next=00000000, before=016768d0, suzuki
292
+
293
+ ###################
294
+
295
+ exchange() 実行前
296
+
297
+ p: addr=01675430, next=01675850, before=01675010, hosi
298
+
299
+ q: addr=01675850, next=01675c70, before=01675430, kato
300
+
301
+ exchange() 実行後
302
+
303
+ p: addr=01675850, next=01675850, before=01675850, kato
304
+
305
+ q: addr=01675430, next=01675c70, before=01675430, hosi
306
+
307
+ ###################
308
+
309
+ 変更後データ表示
310
+
311
+ addr=01675010, next=01675430, before=00000000, yamada
312
+
313
+ addr=01675430, next=01675c70, before=01675430, hosi
314
+
315
+ addr=01675c70, next=01676090, before=01675430, koko
316
+
317
+ addr=01676090, next=016764b0, before=01675c70, naka
318
+
319
+ addr=016764b0, next=016768d0, before=01676090, nakada
320
+
321
+ addr=016768d0, next=01676cf0, before=016764b0, saito
322
+
323
+ addr=01676cf0, next=00000000, before=016768d0, suzuki