質問編集履歴
4
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,6 +4,19 @@
|
|
4
4
|
exchange()関数のところでデータが同じ場合にはそのままreturnして
|
5
5
|
*p,*qをr、sで保存しているところまでは分かるのですが、そのあとが
|
6
6
|
さっぱりわかりません。
|
7
|
+
|
8
|
+
if (&((*p)->next) == q || &((*q)->next) == p)のところで
|
9
|
+
&((*p)->next) == q、&((*q)->next) == pは具体的にどういうことなんでしょうか。
|
10
|
+
|
11
|
+
if (s->next != 0){}の下の文r->before = s;からs->next = r;までは
|
12
|
+
if (s->next == 0){}という条件のことでしょうか。
|
13
|
+
|
14
|
+
if (s->next != 0){
|
15
|
+
s->next->before = r;
|
16
|
+
//「sの次が存在するなら、そいつの手前をrにする」と読める。
|
17
|
+
//つまり" r ← sの次 " ってゆー上り方向のリンクを張り替えたってこと。
|
18
|
+
のようなわかり易い説明をしてもらいました。
|
19
|
+
|
7
20
|
ポインタのポインタは復習してみました。
|
8
21
|
コメントを付けていますが、自分でもよくわかりません。
|
9
22
|
いつも同じような質問で申し訳ありませんが、どなたか説明をしてもらえませんか。
|
@@ -57,24 +70,17 @@
|
|
57
70
|
// q(ポインタのポインタ)と(*p)->nextを比較するには&((*p)->next)とする。
|
58
71
|
|
59
72
|
if (s->next != 0){
|
60
|
-
// sと*qはおなじである。s->nextは(*q)->nextであるから
|
61
|
-
// (*q)->nextが0でないとき
|
62
|
-
|
63
|
-
|
73
|
+
s->next->before = r;
|
64
|
-
//(s->next)->beforeは((*q)->next)->beforeとおなじ
|
65
|
-
//rは*pと同じだから、(s->next)->beforeに*p(これはポインタ)
|
66
|
-
//を代入する。
|
67
|
-
//上り方向のリンクを張り替えたってこと
|
68
|
-
|
74
|
+
//「sの次が存在するなら、そいつの手前をrにする」と読める。
|
69
|
-
|
75
|
+
//つまり" r ← sの次 " ってゆー上り方向のリンクを張り替えたってこと。
|
76
|
+
}
|
70
77
|
r->before = s;
|
71
78
|
//rは*pであるから,*p->before = sである。s==*qなので
|
72
79
|
//*p->beforeに*q(これはポインタ)を代入する。
|
73
80
|
|
74
81
|
s->before = r->before;
|
75
82
|
// s->beforeに*q(これはポインタ)を代入する。
|
76
|
-
|
83
|
+
|
77
|
-
|
78
84
|
*p = s;
|
79
85
|
//sは*qだから*pに*q(これはポインタ)を代入する。
|
80
86
|
|
@@ -106,22 +112,13 @@
|
|
106
112
|
// (r->next)->before=*q
|
107
113
|
}
|
108
114
|
r->before = t;
|
109
|
-
|
115
|
+
|
110
|
-
// t=s->before
|
111
|
-
// r->beforeにs->before==(*q)->before(これはポインタ)を代入する。
|
112
|
-
|
113
116
|
t = r->next;
|
114
|
-
|
117
|
+
|
115
|
-
// t=(*p)->next
|
116
|
-
|
117
118
|
r->next = s->next;
|
118
|
-
|
119
|
+
|
119
|
-
// r->next =(*q)->next
|
120
|
-
|
121
120
|
s->next = t;
|
122
|
-
|
121
|
+
|
123
|
-
// s->next=(*p)->next
|
124
|
-
|
125
122
|
*p = s;
|
126
123
|
*q = r;
|
127
124
|
}
|
3
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,20 +1,17 @@
|
|
1
|
-
構造体のデータを
|
1
|
+
リストの構造体のデータをソートしているコードがあるんですが、
|
2
|
-
exchange()関数のところを図を使って書こうと
|
2
|
+
exchange()関数のところを図を使って書こうと思ったのですが、うまくいきません。
|
3
3
|
図で説明してもらえませんか。
|
4
4
|
exchange()関数のところでデータが同じ場合にはそのままreturnして
|
5
5
|
*p,*qをr、sで保存しているところまでは分かるのですが、そのあとが
|
6
|
+
さっぱりわかりません。
|
6
|
-
|
7
|
+
ポインタのポインタは復習してみました。
|
8
|
+
コメントを付けていますが、自分でもよくわかりません。
|
7
|
-
|
9
|
+
いつも同じような質問で申し訳ありませんが、どなたか説明をしてもらえませんか。
|
8
10
|
よろしくお願いいたします。
|
9
11
|
|
10
12
|
```ここに言語を入力
|
11
|
-
//リスト構造
|
12
|
-
// 電話帳プログラム
|
13
|
+
// 電話帳プログラム
|
13
|
-
|
14
|
+
|
14
|
-
#include <stdlib.h>
|
15
|
-
#include <string.h>
|
16
|
-
#include <assert.h> //
|
17
|
-
|
18
15
|
#define N 256
|
19
16
|
#define FILENAME "address.csv"
|
20
17
|
|
@@ -27,52 +24,8 @@
|
|
27
24
|
struct address *before;
|
28
25
|
};
|
29
26
|
|
30
|
-
void data_show(struct address* head);
|
31
|
-
void data_add(struct address** head);
|
32
|
-
|
27
|
+
//(*head)->nameと(*head)->next)のnameとを比べて
|
33
|
-
void data_sort(struct address** head);
|
34
|
-
void data_write(struct address* head);
|
35
|
-
|
36
|
-
void data_amend(struct address* head);
|
37
|
-
void data_search(struct address* head);
|
38
|
-
|
39
|
-
void chop(char *p){
|
40
|
-
for (; *p; p++)
|
41
|
-
;
|
42
|
-
|
43
|
-
|
28
|
+
//小さいほうのポインタのポインタを返却値にしている。
|
44
|
-
//ポインタを1個戻して,\0 の前が'\r'か'\n'どうか調べるため。
|
45
|
-
|
46
|
-
while (*p == '\r' || *p == '\n')
|
47
|
-
*(p--) = 0;
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
void list_add(struct address **head, char *name, char *address, char *tel, char *mail)
|
52
|
-
{
|
53
|
-
struct address *p;
|
54
|
-
if ((p = malloc(sizeof(struct address))) != 0) {
|
55
|
-
|
56
|
-
strcpy(p->name, name);
|
57
|
-
|
58
|
-
strcpy(p->address, address);
|
59
|
-
//文字型配列 p->address に文字列 address を '\0' までコピーします。
|
60
|
-
|
61
|
-
strcpy(p->tel, tel);
|
62
|
-
//文字型配列 p->number に文字列 number を '\0' までコピーします。
|
63
|
-
|
64
|
-
strcpy(p->mail, mail);
|
65
|
-
//文字型配列 p->mail に文字列 mail を '\0' までコピーします。
|
66
|
-
|
67
|
-
p->next = *head;
|
68
|
-
if (p->next != 0)
|
69
|
-
p->next->before = p;
|
70
|
-
p->before = 0;
|
71
|
-
*head = p;
|
72
|
-
}
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
29
|
struct address **listmin(struct address **head) {
|
77
30
|
struct address **p;
|
78
31
|
if (*head == 0)
|
@@ -92,7 +45,9 @@
|
|
92
45
|
{
|
93
46
|
struct address *r, *s, *t;
|
94
47
|
assert(*p != 0 && *q != 0);
|
95
|
-
|
48
|
+
//assertマクロは関数形式マクロで、引数に偽(すなわち0)が指定されると、
|
49
|
+
//ソースファイル名や行番号等の情報を標準エラー出力に出力し、
|
50
|
+
//プログラムを終了させます。
|
96
51
|
if (p == q)
|
97
52
|
return;
|
98
53
|
|
@@ -107,8 +62,11 @@
|
|
107
62
|
|
108
63
|
s->next->before = r;
|
109
64
|
//(s->next)->beforeは((*q)->next)->beforeとおなじ
|
110
|
-
//rは*pと同じだから、(s->next)->beforeに*p(これはポインタ)
|
65
|
+
//rは*pと同じだから、(s->next)->beforeに*p(これはポインタ)
|
111
|
-
|
66
|
+
//を代入する。
|
67
|
+
//上り方向のリンクを張り替えたってこと
|
68
|
+
} //sの次が存在するなら、そいつの手前をrにする
|
69
|
+
|
112
70
|
r->before = s;
|
113
71
|
//rは*pであるから,*p->before = sである。s==*qなので
|
114
72
|
//*p->beforeに*q(これはポインタ)を代入する。
|
@@ -175,105 +133,22 @@
|
|
175
133
|
if (*head != 0) {
|
176
134
|
for (;;) {
|
177
135
|
p = listmin(head);
|
136
|
+
//(*head)->nameと(*head)->next)のnameとを比べて
|
137
|
+
//小さいほうのポインタのポインタを返却値にしている。
|
138
|
+
//帰ってきたhead(小さいほうのポインタのポインタ),または
|
139
|
+
//p(小さいほうのポインタのポインタ)
|
140
|
+
|
178
141
|
if (p == 0)
|
179
|
-
|
142
|
+
break;
|
180
143
|
|
181
144
|
exchange(head, p);
|
145
|
+
//大小を入れ替える
|
146
|
+
|
182
147
|
head = &((*head)->next);
|
183
148
|
}
|
184
149
|
}
|
185
150
|
}
|
186
151
|
|
187
|
-
void release(struct address **head) {
|
188
|
-
if (*head != 0) {
|
189
|
-
release( &((*head)->next) );
|
190
|
-
free(*head);
|
191
|
-
*head = 0;
|
192
|
-
}
|
193
|
-
}
|
194
|
-
|
195
|
-
int main() {
|
196
|
-
struct address *head;
|
197
|
-
FILE* fp;
|
198
|
-
static char buff[N], name[N], address[N], tel[N], mail[N];
|
199
|
-
char *token=",";
|
200
|
-
int select;
|
201
|
-
|
202
|
-
head = 0;
|
203
|
-
|
204
|
-
if ((fp = fopen(FILENAME,"r")) != 0) {
|
205
|
-
while(fgets(buff, N, fp) != 0){
|
206
|
-
//本当の大元の文字列を書き換えないようにするために
|
207
|
-
//bufを確保してコピーし、それをstrtok()の引数にしている。
|
208
|
-
char *p;
|
209
|
-
chop(buff);
|
210
|
-
printf( "ファイルから読んだ文字列:%s\n", buff );
|
211
|
-
|
212
|
-
p = strtok(buff, token);
|
213
|
-
if ( p != NULL ) {
|
214
|
-
strcpy(name, p);
|
215
|
-
} else {
|
216
|
-
printf( "氏名の切り出しに失敗しました。\n");
|
217
|
-
break;
|
218
|
-
}
|
219
|
-
|
220
|
-
p = strtok(NULL, token);
|
221
|
-
if ( p != NULL ) {
|
222
|
-
strcpy(address, p);
|
223
|
-
} else {
|
224
|
-
printf( "住所の切り出しに失敗しました。\n");
|
225
|
-
break;
|
226
|
-
}
|
227
|
-
|
228
|
-
p = strtok(NULL, token);
|
229
|
-
if ( p != NULL ) {
|
230
|
-
strcpy(tel, p);
|
231
|
-
} else {
|
232
|
-
printf( "電話番号の切り出しに失敗しました。\n");
|
233
|
-
break;
|
234
|
-
}
|
235
|
-
|
236
|
-
p = strtok(NULL, token);
|
237
|
-
if ( p != NULL ) {
|
238
|
-
strcpy(mail, p);
|
239
|
-
} else {
|
240
|
-
printf( "メールアドレスの切り出しに失敗しました。\n");
|
241
|
-
break;
|
242
|
-
}
|
243
|
-
list_add(&head, name, address, tel, mail);
|
244
|
-
}
|
245
|
-
fclose(fp);
|
246
|
-
}
|
247
|
-
while(select != 0){
|
248
|
-
printf(" 1:ソ\ート 2:削除 3:追加 4:全体表\示 5:修正 6:検索 0:終了 \n");
|
249
|
-
|
250
|
-
printf("●メニューを入力して下さい ");
|
251
|
-
fgets(buff, N, stdin);
|
252
|
-
|
253
|
-
if (sscanf(buff, "%d", &select) != 1)
|
254
|
-
|
255
|
-
select = -1;
|
256
|
-
|
257
|
-
switch(select){
|
258
|
-
case 1:
|
259
|
-
data_sort(&head);
|
260
|
-
data_show(head);
|
261
|
-
break;
|
262
|
-
|
263
|
-
......省略.....
|
264
|
-
|
265
|
-
case 0:
|
266
|
-
printf("quit.\n");
|
267
|
-
break;
|
268
|
-
default:
|
269
|
-
printf("bad command, once more.\n");
|
270
|
-
break;
|
271
|
-
}
|
272
|
-
}
|
273
|
-
release(&head);
|
274
|
-
return 0;
|
275
|
-
}
|
276
|
-
|
277
152
|
データ
|
278
153
|
address.csv
|
279
154
|
yamada,tone,090-1122,mail-9
|
2
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,6 @@
|
|
8
8
|
よろしくお願いいたします。
|
9
9
|
|
10
10
|
```ここに言語を入力
|
11
|
-
コード
|
12
11
|
//リスト構造
|
13
12
|
// 電話帳プログラム解説
|
14
13
|
#include <stdio.h>
|
@@ -49,57 +48,6 @@
|
|
49
48
|
|
50
49
|
}
|
51
50
|
|
52
|
-
void data_search(struct address* head)
|
53
|
-
{
|
54
|
-
char buff[N], dummy[N];
|
55
|
-
char *target[] = { "氏名","住所", "電話番号","メールアドレス"};
|
56
|
-
int i,cnt=0,count=0;
|
57
|
-
struct address *t = head;
|
58
|
-
|
59
|
-
while (1) {
|
60
|
-
puts("何で探しますか?\n1.氏名 2.住所 3.電話番号 4.メールアドレス");
|
61
|
-
scanf("%d", &i);
|
62
|
-
|
63
|
-
gets(dummy);
|
64
|
-
if (1 <= i && i <= 4) break;
|
65
|
-
}
|
66
|
-
printf("%sを入力して下さい:", target[--i]);
|
67
|
-
gets(buff);
|
68
|
-
while (t != NULL) {
|
69
|
-
switch (i) {
|
70
|
-
case 0: // 氏名
|
71
|
-
if (!strcmp(t->name, buff)) goto print;
|
72
|
-
break;
|
73
|
-
|
74
|
-
case 1: // 住所
|
75
|
-
if (!strcmp(t->address, buff)) goto print;
|
76
|
-
break;
|
77
|
-
|
78
|
-
case 2: // 電話番号
|
79
|
-
if (!strcmp(t->tel, buff)) goto print;
|
80
|
-
break;
|
81
|
-
|
82
|
-
case 3: // mail
|
83
|
-
if (!strcmp(t->mail, buff)) goto print;
|
84
|
-
break;
|
85
|
-
|
86
|
-
default: // no condition
|
87
|
-
break;
|
88
|
-
}
|
89
|
-
t = t->next;
|
90
|
-
cnt++;
|
91
|
-
continue;
|
92
|
-
|
93
|
-
print:;
|
94
|
-
printf("%d: 氏名:%s 住所:%s 電話番号:%s メールアドレス:%s\n",
|
95
|
-
++cnt, t->name, t->address, t->tel,t->mail);
|
96
|
-
t = t->next;
|
97
|
-
count++;
|
98
|
-
|
99
|
-
}
|
100
|
-
printf("%d件見つかりました\n", count);
|
101
|
-
}
|
102
|
-
|
103
51
|
void list_add(struct address **head, char *name, char *address, char *tel, char *mail)
|
104
52
|
{
|
105
53
|
struct address *p;
|
@@ -124,12 +72,6 @@
|
|
124
72
|
}
|
125
73
|
}
|
126
74
|
|
127
|
-
void data_show(struct address *head) {
|
128
|
-
if (head != 0) {
|
129
|
-
printf(" %s, %s, %s, %s\n", head->name, head->address, head->tel, head->mail);
|
130
|
-
data_show(head->next);
|
131
|
-
}
|
132
|
-
}
|
133
75
|
|
134
76
|
struct address **listmin(struct address **head) {
|
135
77
|
struct address **p;
|
@@ -317,11 +259,9 @@
|
|
317
259
|
data_sort(&head);
|
318
260
|
data_show(head);
|
319
261
|
break;
|
262
|
+
|
320
|
-
|
263
|
+
......省略.....
|
321
|
-
|
264
|
+
|
322
|
-
.....
|
323
|
-
|
324
|
-
|
325
265
|
case 0:
|
326
266
|
printf("quit.\n");
|
327
267
|
break;
|
1
コードの入れ替え
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,11 +9,13 @@
|
|
9
9
|
|
10
10
|
```ここに言語を入力
|
11
11
|
コード
|
12
|
+
//リスト構造
|
13
|
+
// 電話帳プログラム解説
|
12
14
|
#include <stdio.h>
|
13
15
|
#include <stdlib.h>
|
14
16
|
#include <string.h>
|
15
|
-
#include <assert.h>
|
17
|
+
#include <assert.h> //
|
16
|
-
|
18
|
+
|
17
19
|
#define N 256
|
18
20
|
#define FILENAME "address.csv"
|
19
21
|
|
@@ -25,10 +27,110 @@
|
|
25
27
|
struct address *next;
|
26
28
|
struct address *before;
|
27
29
|
};
|
30
|
+
|
28
|
-
|
31
|
+
void data_show(struct address* head);
|
32
|
+
void data_add(struct address** head);
|
33
|
+
void data_delete(struct address** head);
|
29
34
|
void data_sort(struct address** head);
|
35
|
+
void data_write(struct address* head);
|
30
36
|
|
37
|
+
void data_amend(struct address* head);
|
38
|
+
void data_search(struct address* head);
|
31
39
|
|
40
|
+
void chop(char *p){
|
41
|
+
for (; *p; p++)
|
42
|
+
;
|
43
|
+
|
44
|
+
p--; // *pが\0となりfor()を抜けるとここに来る。
|
45
|
+
//ポインタを1個戻して,\0 の前が'\r'か'\n'どうか調べるため。
|
46
|
+
|
47
|
+
while (*p == '\r' || *p == '\n')
|
48
|
+
*(p--) = 0;
|
49
|
+
|
50
|
+
}
|
51
|
+
|
52
|
+
void data_search(struct address* head)
|
53
|
+
{
|
54
|
+
char buff[N], dummy[N];
|
55
|
+
char *target[] = { "氏名","住所", "電話番号","メールアドレス"};
|
56
|
+
int i,cnt=0,count=0;
|
57
|
+
struct address *t = head;
|
58
|
+
|
59
|
+
while (1) {
|
60
|
+
puts("何で探しますか?\n1.氏名 2.住所 3.電話番号 4.メールアドレス");
|
61
|
+
scanf("%d", &i);
|
62
|
+
|
63
|
+
gets(dummy);
|
64
|
+
if (1 <= i && i <= 4) break;
|
65
|
+
}
|
66
|
+
printf("%sを入力して下さい:", target[--i]);
|
67
|
+
gets(buff);
|
68
|
+
while (t != NULL) {
|
69
|
+
switch (i) {
|
70
|
+
case 0: // 氏名
|
71
|
+
if (!strcmp(t->name, buff)) goto print;
|
72
|
+
break;
|
73
|
+
|
74
|
+
case 1: // 住所
|
75
|
+
if (!strcmp(t->address, buff)) goto print;
|
76
|
+
break;
|
77
|
+
|
78
|
+
case 2: // 電話番号
|
79
|
+
if (!strcmp(t->tel, buff)) goto print;
|
80
|
+
break;
|
81
|
+
|
82
|
+
case 3: // mail
|
83
|
+
if (!strcmp(t->mail, buff)) goto print;
|
84
|
+
break;
|
85
|
+
|
86
|
+
default: // no condition
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
t = t->next;
|
90
|
+
cnt++;
|
91
|
+
continue;
|
92
|
+
|
93
|
+
print:;
|
94
|
+
printf("%d: 氏名:%s 住所:%s 電話番号:%s メールアドレス:%s\n",
|
95
|
+
++cnt, t->name, t->address, t->tel,t->mail);
|
96
|
+
t = t->next;
|
97
|
+
count++;
|
98
|
+
|
99
|
+
}
|
100
|
+
printf("%d件見つかりました\n", count);
|
101
|
+
}
|
102
|
+
|
103
|
+
void list_add(struct address **head, char *name, char *address, char *tel, char *mail)
|
104
|
+
{
|
105
|
+
struct address *p;
|
106
|
+
if ((p = malloc(sizeof(struct address))) != 0) {
|
107
|
+
|
108
|
+
strcpy(p->name, name);
|
109
|
+
|
110
|
+
strcpy(p->address, address);
|
111
|
+
//文字型配列 p->address に文字列 address を '\0' までコピーします。
|
112
|
+
|
113
|
+
strcpy(p->tel, tel);
|
114
|
+
//文字型配列 p->number に文字列 number を '\0' までコピーします。
|
115
|
+
|
116
|
+
strcpy(p->mail, mail);
|
117
|
+
//文字型配列 p->mail に文字列 mail を '\0' までコピーします。
|
118
|
+
|
119
|
+
p->next = *head;
|
120
|
+
if (p->next != 0)
|
121
|
+
p->next->before = p;
|
122
|
+
p->before = 0;
|
123
|
+
*head = p;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
void data_show(struct address *head) {
|
128
|
+
if (head != 0) {
|
129
|
+
printf(" %s, %s, %s, %s\n", head->name, head->address, head->tel, head->mail);
|
130
|
+
data_show(head->next);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
32
134
|
struct address **listmin(struct address **head) {
|
33
135
|
struct address **p;
|
34
136
|
if (*head == 0)
|
@@ -36,54 +138,211 @@
|
|
36
138
|
if ((*head)->next == 0)
|
37
139
|
return head;
|
38
140
|
if (strcmp((*head)->name, (*(p = listmin(&((*head)->next))))->name) < 0)
|
141
|
+
// #include <string.h>
|
142
|
+
// int strcmp( const char *str1 , const char *str2 );
|
143
|
+
// str1<str2ならば負の値を返す。
|
39
144
|
return head;
|
40
145
|
else
|
41
146
|
return p;
|
42
147
|
}
|
43
148
|
|
44
|
-
void exchange(struct address **p, struct address **q)
|
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
|
-
|
149
|
+
void exchange(struct address **p, struct address **q)
|
150
|
+
{
|
151
|
+
struct address *r, *s, *t;
|
152
|
+
assert(*p != 0 && *q != 0);
|
153
|
+
|
154
|
+
if (p == q)
|
155
|
+
return;
|
156
|
+
|
157
|
+
r = *p; s = *q;
|
158
|
+
if (&((*p)->next) == q || &((*q)->next) == p) {
|
159
|
+
// (*p)はポインタで,(*p)->nextも次のポインタである。
|
160
|
+
// q(ポインタのポインタ)と(*p)->nextを比較するには&((*p)->next)とする。
|
161
|
+
|
162
|
+
if (s->next != 0){
|
163
|
+
// sと*qはおなじである。s->nextは(*q)->nextであるから
|
164
|
+
// (*q)->nextが0でないとき
|
165
|
+
|
166
|
+
s->next->before = r;
|
167
|
+
//(s->next)->beforeは((*q)->next)->beforeとおなじ
|
168
|
+
//rは*pと同じだから、(s->next)->beforeに*p(これはポインタ)を代入する。
|
169
|
+
}
|
170
|
+
r->before = s;
|
171
|
+
//rは*pであるから,*p->before = sである。s==*qなので
|
172
|
+
//*p->beforeに*q(これはポインタ)を代入する。
|
173
|
+
|
174
|
+
s->before = r->before;
|
175
|
+
// s->beforeに*q(これはポインタ)を代入する。
|
176
|
+
//
|
177
|
+
|
178
|
+
*p = s;
|
179
|
+
//sは*qだから*pに*q(これはポインタ)を代入する。
|
180
|
+
|
181
|
+
*q = s->next;
|
182
|
+
//s->nextは(*q)->nextであるから*qに(*q)->next(これはポインタ)を代入する。
|
183
|
+
|
184
|
+
s->next = r;
|
185
|
+
//rは*pであるから,(*q)->nextに*p(これはポインタ)を代入する。
|
186
|
+
|
187
|
+
return;
|
188
|
+
} else {
|
189
|
+
if (s->next != 0){
|
190
|
+
// (*q)->nextが0でないとき
|
191
|
+
|
192
|
+
s->next->before = r;
|
193
|
+
// (s->next)->before=*p
|
194
|
+
}
|
195
|
+
t = s->before;
|
196
|
+
// s->before==(*q)->before
|
197
|
+
// t=(*q)->before
|
198
|
+
|
199
|
+
s->before = r->before;
|
200
|
+
// s->beforeにr->before==(*p)->before(これはポインタ)を代入する。
|
201
|
+
|
202
|
+
if (r->next != 0){
|
203
|
+
// (*p)->nextが0でないとき
|
204
|
+
|
205
|
+
r->next->before = s;
|
206
|
+
// (r->next)->before=*q
|
207
|
+
}
|
208
|
+
r->before = t;
|
209
|
+
// r->before==(*p)->before
|
210
|
+
// t=s->before
|
211
|
+
// r->beforeにs->before==(*q)->before(これはポインタ)を代入する。
|
212
|
+
|
213
|
+
t = r->next;
|
214
|
+
// r->next==(*p)->nextが0でないときであるから
|
215
|
+
// t=(*p)->next
|
216
|
+
|
217
|
+
r->next = s->next;
|
218
|
+
// s->next==(*q)->next
|
219
|
+
// r->next =(*q)->next
|
220
|
+
|
221
|
+
s->next = t;
|
222
|
+
// t=(*p)->next
|
223
|
+
// s->next=(*p)->next
|
224
|
+
|
225
|
+
*p = s;
|
226
|
+
*q = r;
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
void data_sort(struct address **head)
|
231
|
+
{
|
232
|
+
struct address **p;
|
233
|
+
if (*head != 0) {
|
234
|
+
for (;;) {
|
235
|
+
p = listmin(head);
|
236
|
+
if (p == 0)
|
237
|
+
break;
|
238
|
+
|
239
|
+
exchange(head, p);
|
240
|
+
head = &((*head)->next);
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
void release(struct address **head) {
|
246
|
+
if (*head != 0) {
|
247
|
+
release( &((*head)->next) );
|
248
|
+
free(*head);
|
249
|
+
*head = 0;
|
74
250
|
}
|
75
251
|
}
|
76
252
|
|
77
|
-
|
253
|
+
int main() {
|
78
|
-
struct address *
|
254
|
+
struct address *head;
|
255
|
+
FILE* fp;
|
256
|
+
static char buff[N], name[N], address[N], tel[N], mail[N];
|
257
|
+
char *token=",";
|
258
|
+
int select;
|
259
|
+
|
260
|
+
head = 0;
|
261
|
+
|
262
|
+
if ((fp = fopen(FILENAME,"r")) != 0) {
|
263
|
+
while(fgets(buff, N, fp) != 0){
|
264
|
+
//本当の大元の文字列を書き換えないようにするために
|
265
|
+
//bufを確保してコピーし、それをstrtok()の引数にしている。
|
266
|
+
char *p;
|
267
|
+
chop(buff);
|
268
|
+
printf( "ファイルから読んだ文字列:%s\n", buff );
|
269
|
+
|
270
|
+
p = strtok(buff, token);
|
79
|
-
|
271
|
+
if ( p != NULL ) {
|
272
|
+
strcpy(name, p);
|
80
|
-
|
273
|
+
} else {
|
81
|
-
|
274
|
+
printf( "氏名の切り出しに失敗しました。\n");
|
82
|
-
if (p == 0)
|
83
|
-
|
275
|
+
break;
|
276
|
+
}
|
277
|
+
|
278
|
+
p = strtok(NULL, token);
|
279
|
+
if ( p != NULL ) {
|
84
|
-
|
280
|
+
strcpy(address, p);
|
281
|
+
} else {
|
282
|
+
printf( "住所の切り出しに失敗しました。\n");
|
283
|
+
break;
|
284
|
+
}
|
285
|
+
|
85
|
-
|
286
|
+
p = strtok(NULL, token);
|
287
|
+
if ( p != NULL ) {
|
288
|
+
strcpy(tel, p);
|
289
|
+
} else {
|
290
|
+
printf( "電話番号の切り出しに失敗しました。\n");
|
291
|
+
break;
|
292
|
+
}
|
293
|
+
|
294
|
+
p = strtok(NULL, token);
|
295
|
+
if ( p != NULL ) {
|
296
|
+
strcpy(mail, p);
|
297
|
+
} else {
|
298
|
+
printf( "メールアドレスの切り出しに失敗しました。\n");
|
299
|
+
break;
|
300
|
+
}
|
301
|
+
list_add(&head, name, address, tel, mail);
|
302
|
+
}
|
303
|
+
fclose(fp);
|
304
|
+
}
|
305
|
+
while(select != 0){
|
306
|
+
printf(" 1:ソ\ート 2:削除 3:追加 4:全体表\示 5:修正 6:検索 0:終了 \n");
|
307
|
+
|
308
|
+
printf("●メニューを入力して下さい ");
|
309
|
+
fgets(buff, N, stdin);
|
310
|
+
|
311
|
+
if (sscanf(buff, "%d", &select) != 1)
|
312
|
+
|
313
|
+
select = -1;
|
314
|
+
|
315
|
+
switch(select){
|
316
|
+
case 1:
|
317
|
+
data_sort(&head);
|
318
|
+
data_show(head);
|
319
|
+
break;
|
320
|
+
.....
|
321
|
+
省略
|
322
|
+
.....
|
323
|
+
|
324
|
+
|
325
|
+
case 0:
|
326
|
+
printf("quit.\n");
|
327
|
+
break;
|
328
|
+
default:
|
329
|
+
printf("bad command, once more.\n");
|
330
|
+
break;
|
86
331
|
}
|
87
332
|
}
|
333
|
+
release(&head);
|
334
|
+
return 0;
|
88
335
|
}
|
336
|
+
|
337
|
+
データ
|
338
|
+
address.csv
|
339
|
+
yamada,tone,090-1122,mail-9
|
340
|
+
hosi,nagoya,5436,mail-7
|
341
|
+
kato,kanagawa,080-8888,mail-2
|
342
|
+
koko,yosida,090-2314,mail-6
|
343
|
+
naka,kamikosaka,080-4444,mail-1
|
344
|
+
nakada,nogata,090-6376,mail-8
|
345
|
+
saito,yamanashi,080-6666,mail-3
|
346
|
+
suzuki,saitama,090-2222,mail-5
|
347
|
+
|
89
348
|
```
|