質問編集履歴
1
タイトルとコードの修正をしました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
リストのソートがうまくいきません
|
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
|
3
|
+
コード
|
4
|
+
|
1
|
-
### 前提・実現したいこと
|
5
|
+
```### 前提・実現したいこと
|
2
6
|
|
3
7
|
ノードを用いたソートの関数の実現
|
4
8
|
|
@@ -22,318 +26,316 @@
|
|
22
26
|
|
23
27
|
|
24
28
|
|
25
|
-
```ここに言語名を入力
|
26
|
-
|
27
|
-
C言語
|
29
|
+
```C言語
|
30
|
+
|
28
|
-
|
31
|
+
/*(ヘッダ部)*/
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
#ifndef MYNODE_H
|
36
|
+
|
37
|
+
#define MYNODE_H
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
#include<stdio.h>
|
42
|
+
|
43
|
+
#include<stdlib.h>
|
44
|
+
|
45
|
+
#include<string.h>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
/*--- 生徒データ ---*/
|
50
|
+
|
51
|
+
typedef struct {
|
52
|
+
|
53
|
+
char name[20]; /* 氏名 */
|
54
|
+
|
55
|
+
int math; /*数学*/
|
56
|
+
|
57
|
+
int eng; /*英語*/
|
58
|
+
|
59
|
+
} Student;
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
/*--- ノード ---*/
|
64
|
+
|
65
|
+
typedef struct __node {
|
66
|
+
|
67
|
+
Student data; /* データ */
|
68
|
+
|
69
|
+
struct __node *next; /* 後続ポインタ(後続ノードへのポインタ)*/
|
70
|
+
|
71
|
+
} Node;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
/*--- 線形リスト ---*/
|
76
|
+
|
77
|
+
typedef struct {
|
78
|
+
|
79
|
+
Node *head; /* 先頭ノードへのポインタ */
|
80
|
+
|
81
|
+
Node *crnt; /* 着目ノードへのポインタ */
|
82
|
+
|
83
|
+
} List;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
/*--- 線形リストを初期化 ---*/
|
88
|
+
|
89
|
+
void Initialize(List *list)
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
list->head = NULL; /* 先頭ノード */
|
94
|
+
|
95
|
+
list->crnt = NULL; /* 着目ノード */
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
/*--- 一つのノードを動的に生成 ---*/
|
100
|
+
|
101
|
+
Node *AllocNode(void)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
if(calloc(1,sizeof(Node)) == NULL)
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
puts("記憶域の確保に失敗しました");
|
110
|
+
|
111
|
+
return NULL;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
else
|
116
|
+
|
117
|
+
return calloc(1,sizeof(Node));
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
/*--- nの指すノードの各メンバに値を設定 ----*/
|
122
|
+
|
123
|
+
void SetNode(Node *n, Student *x, Node *next)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
n->data = *x; /* データ */
|
128
|
+
|
129
|
+
n->next = next; /* 後続ポインタ */
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
/*--- 生徒データの表示(改行あり)---*/
|
134
|
+
|
135
|
+
void PrintLnStudent(const Student *x)
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
printf("Name:%s Math:%d Eng:%d\n", x->name, x->math, x->eng);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
/*--- 先頭にノードを挿入 ---*/
|
144
|
+
|
145
|
+
void InsertFront(List *list, Student *x)
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
Node *ptr = list->head;
|
150
|
+
|
151
|
+
list->head = list->crnt = AllocNode();
|
152
|
+
|
153
|
+
SetNode(list->head, x, ptr);
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
/*--- 全ノードのデータをリスト順に表示 ---*/
|
158
|
+
|
159
|
+
void Print(const List *list)
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
if (list->head == NULL)
|
164
|
+
|
165
|
+
puts("ノードがありません。");
|
166
|
+
|
167
|
+
else {
|
168
|
+
|
169
|
+
Node *ptr = list->head;
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
puts("全データを先頭ノードから順番に表示する");
|
174
|
+
|
175
|
+
while (ptr != NULL) {
|
176
|
+
|
177
|
+
PrintLnStudent(&ptr->data);
|
178
|
+
|
179
|
+
ptr = ptr->next; /* 後続ノードに着目 */
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
//ソートの関数
|
188
|
+
|
189
|
+
void math_sort(List *list)
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
int min,score;
|
194
|
+
|
195
|
+
Student tmp;
|
196
|
+
|
197
|
+
Node *phead = list->head;
|
198
|
+
|
199
|
+
Node *ptr_i = list->head;
|
200
|
+
|
201
|
+
Node *ptr_j;
|
202
|
+
|
203
|
+
Node *ptr_min;
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
while(ptr_i->next != NULL)
|
208
|
+
|
209
|
+
{
|
210
|
+
|
211
|
+
ptr_j = ptr_i;
|
212
|
+
|
213
|
+
ptr_min = ptr_i;
|
214
|
+
|
215
|
+
min = ptr_i->data.math;
|
216
|
+
|
217
|
+
//最小値を探索
|
218
|
+
|
219
|
+
while(ptr_j->next != NULL)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
ptr_j= ptr_j->next;
|
224
|
+
|
225
|
+
score = ptr_j->data.math;
|
226
|
+
|
227
|
+
if(min > score)
|
228
|
+
|
229
|
+
{
|
230
|
+
|
231
|
+
min = score;
|
232
|
+
|
233
|
+
//着目ノードを変更
|
234
|
+
|
235
|
+
list->crnt = ptr_j;
|
236
|
+
|
237
|
+
ptr_min = ptr_j;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
//最小値がptr_iにない場合
|
244
|
+
|
245
|
+
if(ptr_min != ptr_i)
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
tmp = ptr_min->data;
|
250
|
+
|
251
|
+
RemoveCurrent(list);
|
252
|
+
|
253
|
+
if(ptr_i == list->head)
|
254
|
+
|
255
|
+
{
|
256
|
+
|
257
|
+
InsertFront(list,&tmp);
|
258
|
+
|
259
|
+
ptr_i = ptr_i->next;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
else
|
264
|
+
|
265
|
+
{
|
266
|
+
|
267
|
+
phead = phead->next = ptr_i;
|
268
|
+
|
269
|
+
ptr_i = ptr_i->next;
|
270
|
+
|
271
|
+
phead->next = AllocNode();
|
272
|
+
|
273
|
+
SetNode(phead->next,&tmp,ptr_i);
|
274
|
+
|
275
|
+
ptr_i = ptr_i->next;
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
//最小値がptr_iにある場合
|
282
|
+
|
283
|
+
else
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
phead = phead->next;
|
288
|
+
|
289
|
+
ptr_i = ptr_i->next;
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
#endif
|
302
|
+
|
303
|
+
|
304
|
+
|
29
|
-
ソース
|
305
|
+
ソースファイル
|
306
|
+
|
307
|
+
#include<stdio.h>
|
308
|
+
|
309
|
+
#include"mynode.h"
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
int main(void)
|
314
|
+
|
315
|
+
{
|
316
|
+
|
317
|
+
List list;
|
318
|
+
|
319
|
+
Initialize(&list);
|
320
|
+
|
321
|
+
fset_all_Student(&list,"input.txt");
|
322
|
+
|
323
|
+
Print(&list);
|
324
|
+
|
325
|
+
math_sort(&list);
|
326
|
+
|
327
|
+
Print(&list);
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
return 0;
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
30
336
|
|
31
337
|
```
|
32
338
|
|
33
|
-
/*(ヘッダ部)*/
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
#ifndef MYNODE_H
|
38
|
-
|
39
|
-
#define MYNODE_H
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
#include<stdio.h>
|
44
|
-
|
45
|
-
#include<stdlib.h>
|
46
|
-
|
47
|
-
#include<string.h>
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
/*--- 生徒データ ---*/
|
52
|
-
|
53
|
-
typedef struct {
|
54
|
-
|
55
|
-
char name[20]; /* 氏名 */
|
56
|
-
|
57
|
-
int math; /*数学*/
|
58
|
-
|
59
|
-
int eng; /*英語*/
|
60
|
-
|
61
|
-
} Student;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
/*--- ノード ---*/
|
66
|
-
|
67
|
-
typedef struct __node {
|
68
|
-
|
69
|
-
Student data; /* データ */
|
70
|
-
|
71
|
-
struct __node *next; /* 後続ポインタ(後続ノードへのポインタ)*/
|
72
|
-
|
73
|
-
} Node;
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
/*--- 線形リスト ---*/
|
78
|
-
|
79
|
-
typedef struct {
|
80
|
-
|
81
|
-
Node *head; /* 先頭ノードへのポインタ */
|
82
|
-
|
83
|
-
Node *crnt; /* 着目ノードへのポインタ */
|
84
|
-
|
85
|
-
} List;
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
/*--- 線形リストを初期化 ---*/
|
90
|
-
|
91
|
-
void Initialize(List *list)
|
92
|
-
|
93
|
-
{
|
94
|
-
|
95
|
-
list->head = NULL; /* 先頭ノード */
|
96
|
-
|
97
|
-
list->crnt = NULL; /* 着目ノード */
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
/*--- 一つのノードを動的に生成 ---*/
|
102
|
-
|
103
|
-
Node *AllocNode(void)
|
104
|
-
|
105
|
-
{
|
106
|
-
|
107
|
-
if(calloc(1,sizeof(Node)) == NULL)
|
108
|
-
|
109
|
-
{
|
110
|
-
|
111
|
-
puts("記憶域の確保に失敗しました");
|
112
|
-
|
113
|
-
return NULL;
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
else
|
118
|
-
|
119
|
-
return calloc(1,sizeof(Node));
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
/*--- nの指すノードの各メンバに値を設定 ----*/
|
124
|
-
|
125
|
-
void SetNode(Node *n, Student *x, Node *next)
|
126
|
-
|
127
|
-
{
|
128
|
-
|
129
|
-
n->data = *x; /* データ */
|
130
|
-
|
131
|
-
n->next = next; /* 後続ポインタ */
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
/*--- 生徒データの表示(改行あり)---*/
|
136
|
-
|
137
|
-
void PrintLnStudent(const Student *x)
|
138
|
-
|
139
|
-
{
|
140
|
-
|
141
|
-
printf("Name:%s Math:%d Eng:%d\n", x->name, x->math, x->eng);
|
142
|
-
|
143
|
-
}
|
144
|
-
|
145
|
-
/*--- 先頭にノードを挿入 ---*/
|
146
|
-
|
147
|
-
void InsertFront(List *list, Student *x)
|
148
|
-
|
149
|
-
{
|
150
|
-
|
151
|
-
Node *ptr = list->head;
|
152
|
-
|
153
|
-
list->head = list->crnt = AllocNode();
|
154
|
-
|
155
|
-
SetNode(list->head, x, ptr);
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
/*--- 全ノードのデータをリスト順に表示 ---*/
|
160
|
-
|
161
|
-
void Print(const List *list)
|
162
|
-
|
163
|
-
{
|
164
|
-
|
165
|
-
if (list->head == NULL)
|
166
|
-
|
167
|
-
puts("ノードがありません。");
|
168
|
-
|
169
|
-
else {
|
170
|
-
|
171
|
-
Node *ptr = list->head;
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
puts("全データを先頭ノードから順番に表示する");
|
176
|
-
|
177
|
-
while (ptr != NULL) {
|
178
|
-
|
179
|
-
PrintLnStudent(&ptr->data);
|
180
|
-
|
181
|
-
ptr = ptr->next; /* 後続ノードに着目 */
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
//ソートの関数
|
190
|
-
|
191
|
-
void math_sort(List *list)
|
192
|
-
|
193
|
-
{
|
194
|
-
|
195
|
-
int min,score;
|
196
|
-
|
197
|
-
Student tmp;
|
198
|
-
|
199
|
-
Node *phead = list->head;
|
200
|
-
|
201
|
-
Node *ptr_i = list->head;
|
202
|
-
|
203
|
-
Node *ptr_j;
|
204
|
-
|
205
|
-
Node *ptr_min;
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
while(ptr_i->next != NULL)
|
210
|
-
|
211
|
-
{
|
212
|
-
|
213
|
-
ptr_j = ptr_i;
|
214
|
-
|
215
|
-
ptr_min = ptr_i;
|
216
|
-
|
217
|
-
min = ptr_i->data.math;
|
218
|
-
|
219
|
-
//最小値を探索
|
220
|
-
|
221
|
-
while(ptr_j->next != NULL)
|
222
|
-
|
223
|
-
{
|
224
|
-
|
225
|
-
ptr_j= ptr_j->next;
|
226
|
-
|
227
|
-
score = ptr_j->data.math;
|
228
|
-
|
229
|
-
if(min > score)
|
230
|
-
|
231
|
-
{
|
232
|
-
|
233
|
-
min = score;
|
234
|
-
|
235
|
-
//着目ノードを変更
|
236
|
-
|
237
|
-
list->crnt = ptr_j;
|
238
|
-
|
239
|
-
ptr_min = ptr_j;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
//最小値がptr_iにない場合
|
246
|
-
|
247
|
-
if(ptr_min != ptr_i)
|
248
|
-
|
249
|
-
{
|
250
|
-
|
251
|
-
tmp = ptr_min->data;
|
252
|
-
|
253
|
-
RemoveCurrent(list);
|
254
|
-
|
255
|
-
if(ptr_i == list->head)
|
256
|
-
|
257
|
-
{
|
258
|
-
|
259
|
-
InsertFront(list,&tmp);
|
260
|
-
|
261
|
-
ptr_i = ptr_i->next;
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
else
|
266
|
-
|
267
|
-
{
|
268
|
-
|
269
|
-
phead = phead->next = ptr_i;
|
270
|
-
|
271
|
-
ptr_i = ptr_i->next;
|
272
|
-
|
273
|
-
phead->next = AllocNode();
|
274
|
-
|
275
|
-
SetNode(phead->next,&tmp,ptr_i);
|
276
|
-
|
277
|
-
ptr_i = ptr_i->next;
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
//最小値がptr_iにある場合
|
284
|
-
|
285
|
-
else
|
286
|
-
|
287
|
-
{
|
288
|
-
|
289
|
-
phead = phead->next;
|
290
|
-
|
291
|
-
ptr_i = ptr_i->next;
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
#endif
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
ソースファイル
|
308
|
-
|
309
|
-
#include<stdio.h>
|
310
|
-
|
311
|
-
#include"mynode.h"
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
int main(void)
|
316
|
-
|
317
|
-
{
|
318
|
-
|
319
|
-
List list;
|
320
|
-
|
321
|
-
Initialize(&list);
|
322
|
-
|
323
|
-
fset_all_Student(&list,"input.txt");
|
324
|
-
|
325
|
-
Print(&list);
|
326
|
-
|
327
|
-
math_sort(&list);
|
328
|
-
|
329
|
-
Print(&list);
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
return 0;
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
339
|
### 試したこと
|
338
340
|
|
339
341
|
ファイルからの読み込みには成功していました。
|