質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,3 +13,269 @@
|
|
13
13
|
交換にあたるコードがわからない。
|
14
14
|
|
15
15
|
また、書いてみたが無限ループや完璧でないソートになった。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
### 該当のソースコード
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```c
|
24
|
+
|
25
|
+
#include <stdio.h>
|
26
|
+
|
27
|
+
#include <stdlib.h>
|
28
|
+
|
29
|
+
#include <time.h>
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
typedef struct node{
|
34
|
+
|
35
|
+
struct node *prev;
|
36
|
+
|
37
|
+
struct node *next;
|
38
|
+
|
39
|
+
int value;
|
40
|
+
|
41
|
+
}NODE;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
#define DUMMY -1
|
46
|
+
|
47
|
+
#define MAX 99
|
48
|
+
|
49
|
+
#define NUM 10
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
NODE* make_list(int num);
|
54
|
+
|
55
|
+
NODE* make_cell(int val);
|
56
|
+
|
57
|
+
void insert_cell_to_list(NODE *x, NODE *p);
|
58
|
+
|
59
|
+
NODE* delete_cell_from_list(NODE *x);
|
60
|
+
|
61
|
+
void print_list(NODE *link_list_head);
|
62
|
+
|
63
|
+
void free_list(NODE *link_list_head);
|
64
|
+
|
65
|
+
void selection_sort_cell(NODE *link_list_head);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
int main(void){
|
70
|
+
|
71
|
+
NODE *list_head;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
srand((unsigned int)time(NULL));
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
list_head=make_list(NUM);
|
80
|
+
|
81
|
+
printf("ランダムで作成したリストを表示\n");
|
82
|
+
|
83
|
+
print_list(list_head);
|
84
|
+
|
85
|
+
selection_sort_cell(list_head);
|
86
|
+
|
87
|
+
printf("選択ソート後\n");
|
88
|
+
|
89
|
+
print_list(list_head);
|
90
|
+
|
91
|
+
printf("\n");
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
free_list(list_head);
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
return 0;
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
NODE* make_list(int num){
|
106
|
+
|
107
|
+
NODE *list, *cel, *list_head;
|
108
|
+
|
109
|
+
int i;
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
list=make_cell(DUMMY);
|
114
|
+
|
115
|
+
list->next=list;
|
116
|
+
|
117
|
+
list->prev=list;
|
118
|
+
|
119
|
+
list_head=list;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
for(i=0; i<num; i++){
|
124
|
+
|
125
|
+
cel=make_cell(rand()%MAX);
|
126
|
+
|
127
|
+
list=list->next;
|
128
|
+
|
129
|
+
insert_cell_to_list(cel, list);
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
return (list_head);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
NODE* make_cell(int val){
|
140
|
+
|
141
|
+
NODE *p;
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
p=(NODE*)malloc(sizeof(NODE));
|
146
|
+
|
147
|
+
if (p == NULL) {
|
148
|
+
|
149
|
+
fprintf(stderr, "Memory allocation error!\n");
|
150
|
+
|
151
|
+
exit(-1);
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
p->value =val;
|
156
|
+
|
157
|
+
p->next =NULL;
|
158
|
+
|
159
|
+
p->prev =NULL;
|
160
|
+
|
161
|
+
return(p);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
void insert_cell_to_list(NODE *x, NODE *p){
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
x->prev = p;
|
172
|
+
|
173
|
+
x->next = p->next;
|
174
|
+
|
175
|
+
p->next->prev = x;
|
176
|
+
|
177
|
+
p->next = x;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
NODE* delete_cell_from_list(NODE *x){
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
x->prev->next = x->next;
|
188
|
+
|
189
|
+
x->next->prev = x->prev;
|
190
|
+
|
191
|
+
return (x);
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
void print_list(NODE *link_list_head){
|
198
|
+
|
199
|
+
NODE *p;
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
for (p = link_list_head->next; p != link_list_head; p = p->next) {
|
204
|
+
|
205
|
+
printf("%d\n", p->value);
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
void free_list(NODE *link_list_head){
|
214
|
+
|
215
|
+
NODE *p, *q;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
p=link_list_head;
|
220
|
+
|
221
|
+
p->prev->next = NULL;
|
222
|
+
|
223
|
+
while (p!=NULL){
|
224
|
+
|
225
|
+
q=p->next;
|
226
|
+
|
227
|
+
free(p);
|
228
|
+
|
229
|
+
p=q;
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
void selection_sort_cell(NODE *link_list_head){
|
238
|
+
|
239
|
+
NODE *p, *q, *low, *tmp;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
for (p = link_list_head->next; p != link_list_head->prev; p = p->next) {
|
244
|
+
|
245
|
+
low = p;
|
246
|
+
|
247
|
+
for (q = p->next; q != link_list_head; q = q->next) {
|
248
|
+
|
249
|
+
if (low->value > q->value) {
|
250
|
+
|
251
|
+
low = q;
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
/* ここからソート(未完成) */
|
258
|
+
|
259
|
+
tmp = delete_cell_from_list(low);
|
260
|
+
|
261
|
+
insert_cell_to_list(tmp, p);
|
262
|
+
|
263
|
+
tmp = delete_cell_from_list(p);
|
264
|
+
|
265
|
+
insert_cell_to_list(tmp, low->prev);
|
266
|
+
|
267
|
+
p = p->next;
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
### 試したこと
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
ソート部分を自分が考えたように書いたが、うまくいかなかった。
|