回答編集履歴
2
オリジナルの構造に戻しました
test
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
|
27
27
|
} CELL;
|
28
28
|
|
29
|
-
CELL *head;
|
29
|
+
CELL *head = NULL;
|
30
30
|
|
31
31
|
|
32
32
|
|
@@ -38,33 +38,9 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
|
41
|
+
void insert_cell(CELL *p, int d) {
|
42
42
|
|
43
|
-
int isOdd(int v) { return v%2; }
|
44
|
-
|
45
|
-
//奇数の挿入位置で !0 を返す.
|
46
|
-
|
47
|
-
int odd_asc(int a, int b) { return isOdd(b) && a<b; }
|
48
|
-
|
49
|
-
//偶数の挿入位置で !0 を返す.
|
50
|
-
|
51
|
-
int even_desc(int a, int b) { return isOdd(b) || a>b; }
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
// order が !0 を返した位置に d を挿入する
|
56
|
-
|
57
|
-
void insert_cell(CELL *head, int d, int (*order)(int, int)) {
|
58
|
-
|
59
|
-
CELL *p = head;
|
60
|
-
|
61
|
-
for( ; p->next != NULL; p=p->next) {
|
62
|
-
|
63
|
-
if(order(d, p->next->data)) break;
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
CELL *new_cell = (CELL*)malloc(sizeof(CELL));
|
43
|
+
CELL *new_cell = (CELL *)malloc(sizeof(CELL));
|
68
44
|
|
69
45
|
new_cell->data = d;
|
70
46
|
|
@@ -76,13 +52,63 @@
|
|
76
52
|
|
77
53
|
|
78
54
|
|
55
|
+
//奇数なら !0 を返す.
|
56
|
+
|
57
|
+
int isOdd(int v) { return v%2; }
|
58
|
+
|
59
|
+
//奇数の挿入位置で !0 を返す.
|
60
|
+
|
61
|
+
int odd_order(int a, int b) { return isOdd(b) && a<b; }
|
62
|
+
|
63
|
+
//偶数の挿入位置で !0 を返す.
|
64
|
+
|
65
|
+
int even_order(int a, int b) { return isOdd(b) || a>b; }
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
void insert_data(int d) {
|
70
|
+
|
71
|
+
printf("insert_start\n");
|
72
|
+
|
73
|
+
int (*order)(int, int) = isOdd(d) ? odd_order : even_order;
|
74
|
+
|
75
|
+
CELL h = { .next = head }; //先頭への挿入の簡略化のため
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
CELL *p = &h;
|
80
|
+
|
81
|
+
for( ; p->next != NULL; p=p->next) {
|
82
|
+
|
83
|
+
if(order(d, p->next->data)) break;
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
printf("pointer_set\n");
|
88
|
+
|
89
|
+
insert_cell(p, d);
|
90
|
+
|
91
|
+
head = h.next;
|
92
|
+
|
93
|
+
printf("insert_end\n");
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
79
99
|
int main(void) {
|
100
|
+
|
101
|
+
clock_t start, end;
|
102
|
+
|
103
|
+
int i;
|
80
104
|
|
81
105
|
int x[10];
|
82
106
|
|
107
|
+
|
108
|
+
|
83
109
|
srand(time(NULL));
|
84
110
|
|
85
|
-
for(i
|
111
|
+
for(i=0; i<10; i++) {
|
86
112
|
|
87
113
|
x[i] = rand() % 10;
|
88
114
|
|
@@ -92,23 +118,15 @@
|
|
92
118
|
|
93
119
|
|
94
120
|
|
95
|
-
|
121
|
+
start = clock();
|
96
122
|
|
97
|
-
|
123
|
+
for(i=0; i<10; i++) {
|
98
124
|
|
99
|
-
|
125
|
+
insert_data(x[i]);
|
100
|
-
|
101
|
-
for(int i=0; i<10; i++) {
|
102
|
-
|
103
|
-
insert_cell(&h, x[i], isOdd(x[i])?odd_asc:even_desc);
|
104
126
|
|
105
127
|
}
|
106
128
|
|
107
|
-
head = h.next;
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
129
|
+
end = clock();
|
112
130
|
|
113
131
|
printf("%f sec\n", (double)(end-start)/CLOCKS_PER_SEC);
|
114
132
|
|
1
コピペミスかコンパイルエラーがあったので修正と, 1本のリストで出来たので回答を修正
test
CHANGED
@@ -1,16 +1,20 @@
|
|
1
|
-
一つのリストに対し何度もループするより, 偶数奇数でそれぞれリストを作って繋げるほうが, コードがすっきりしてNULLチェック抜け等しにくくなるかと思います.
|
1
|
+
~~一つのリストに対し何度もループするより, 偶数奇数でそれぞれリストを作って繋げるほうが, コードがすっきりしてNULLチェック抜け等しにくくなるかと思います.
|
2
2
|
|
3
|
-
最後に繋げる必要が出てきますが.
|
3
|
+
最後に繋げる必要が出てきますが.~~
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
お風呂で考え直しましたら, 並び替え条件をちゃんと考えれば1本のリストでもすっきりできました.
|
4
8
|
|
5
9
|
|
6
10
|
|
7
11
|
```c
|
8
12
|
|
9
|
-
#include<stdio.h>
|
13
|
+
#include <stdio.h>
|
10
14
|
|
11
|
-
#include<stdlib.h>
|
15
|
+
#include <stdlib.h>
|
12
16
|
|
13
|
-
#include<time.h>
|
17
|
+
#include <time.h>
|
14
18
|
|
15
19
|
|
16
20
|
|
@@ -34,15 +38,27 @@
|
|
34
38
|
|
35
39
|
|
36
40
|
|
37
|
-
|
41
|
+
//奇数なら !0 を返す.
|
38
42
|
|
43
|
+
int isOdd(int v) { return v%2; }
|
44
|
+
|
45
|
+
//奇数の挿入位置で !0 を返す.
|
46
|
+
|
47
|
+
int odd_asc(int a, int b) { return isOdd(b) && a<b; }
|
48
|
+
|
49
|
+
//偶数の挿入位置で !0 を返す.
|
50
|
+
|
39
|
-
int desc
|
51
|
+
int even_desc(int a, int b) { return isOdd(b) || a>b; }
|
40
52
|
|
41
53
|
|
42
54
|
|
55
|
+
// order が !0 を返した位置に d を挿入する
|
56
|
+
|
43
57
|
void insert_cell(CELL *head, int d, int (*order)(int, int)) {
|
44
58
|
|
59
|
+
CELL *p = head;
|
60
|
+
|
45
|
-
for(
|
61
|
+
for( ; p->next != NULL; p=p->next) {
|
46
62
|
|
47
63
|
if(order(d, p->next->data)) break;
|
48
64
|
|
@@ -55,22 +71,6 @@
|
|
55
71
|
new_cell->next = p->next;
|
56
72
|
|
57
73
|
p->next = new_cell;
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
//first(のnext)が指すリストの後に second(のnext)が指すリストを繋ぎ, リストの先頭の返す.
|
64
|
-
|
65
|
-
CELL *collect(CELL first, CELL second) {
|
66
|
-
|
67
|
-
CELL *p = &first;
|
68
|
-
|
69
|
-
while(p->next != NULL) p = p->next;
|
70
|
-
|
71
|
-
p->next = second.next;
|
72
|
-
|
73
|
-
return first.next;
|
74
74
|
|
75
75
|
}
|
76
76
|
|
@@ -94,29 +94,17 @@
|
|
94
94
|
|
95
95
|
clock_t start = clock();
|
96
96
|
|
97
|
+
CELL h; //自身はデータを持たず, next がリストの先頭を指す
|
97
98
|
|
98
|
-
|
99
|
-
CELL odd, even; //自身はデータを持たず, next がリストの先頭を指す.
|
100
|
-
|
101
|
-
|
99
|
+
h.next = NULL;
|
102
|
-
|
103
|
-
even.next = NULL;
|
104
100
|
|
105
101
|
for(int i=0; i<10; i++) {
|
106
102
|
|
107
|
-
if(x[i]%2 == 0) {
|
108
|
-
|
109
|
-
insert_cell(&even, x[i], descending); //偶数降順
|
110
|
-
|
111
|
-
} else {
|
112
|
-
|
113
|
-
|
103
|
+
insert_cell(&h, x[i], isOdd(x[i])?odd_asc:even_desc);
|
114
|
-
|
115
|
-
}
|
116
104
|
|
117
105
|
}
|
118
106
|
|
119
|
-
head =
|
107
|
+
head = h.next;
|
120
108
|
|
121
109
|
|
122
110
|
|