質問編集履歴
2
ソースコードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,89 +8,203 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
[追記]
|
12
|
+
|
13
|
+
ご教授のおかげで実行結果はほぼ正しくなったのですが, データを降順に並べたあとに必ず0とSegmentation fault: 11が出ます.
|
14
|
+
|
15
|
+
何度も申し訳ないのですが原因が分からないのでご教授お願いします.
|
16
|
+
|
17
|
+
|
18
|
+
|
11
19
|
|
12
20
|
|
13
21
|
###該当のソースコード
|
14
22
|
|
15
23
|
```C
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
return n
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
ret
|
25
|
+
#include<stdio.h>
|
26
|
+
|
27
|
+
#include<stdlib.h>
|
28
|
+
|
29
|
+
#include<time.h>
|
30
|
+
|
31
|
+
#define SUCCESS 1
|
32
|
+
|
33
|
+
#define FAILURE 0
|
34
|
+
|
35
|
+
#define N 20
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
typedef int data_t; //データ
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
typedef struct nodetag //ノードの定義
|
44
|
+
|
45
|
+
{
|
46
|
+
|
47
|
+
data_t data;
|
48
|
+
|
49
|
+
struct nodetag *next;
|
50
|
+
|
51
|
+
} node_t;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
node_t *nodeNew(data_t dt, node_t *nxt) //ノードの新規作成
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
node_t *ndPtr;
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
ndPtr = malloc(sizeof (node_t)); //ヒープを利用
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
if (ndPtr == NULL) {
|
68
|
+
|
69
|
+
return NULL;
|
70
|
+
|
71
|
+
} else {
|
72
|
+
|
73
|
+
ndPtr->data = dt;
|
74
|
+
|
75
|
+
ndPtr->next = nxt;
|
76
|
+
|
77
|
+
return ndPtr;
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
void listPrint(node_t *ndPtr) //データ内容の表示
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
while (ndPtr != NULL) {
|
90
|
+
|
91
|
+
printf("%d\n", ndPtr->data);
|
92
|
+
|
93
|
+
ndPtr = ndPtr->next;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
int nodeDes(node_t *ndPtr, node_t *nd) //ndが与えられたリストで何番目に大きいか判定
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
int cnt = 0;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
while (ndPtr != NULL && ndPtr->data > nd->data) {
|
110
|
+
|
111
|
+
if (ndPtr->data == nd->data) return -1;
|
112
|
+
|
113
|
+
cnt++;
|
114
|
+
|
115
|
+
ndPtr = ndPtr->next;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
return cnt;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
int nodeInsert(node_t **ndPtrPtr, int n, data_t dt)
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
int i;
|
132
|
+
|
133
|
+
node_t *ndPtr;
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
if (n < 0) return FAILURE;
|
138
|
+
|
139
|
+
for (i = 0; i < n && *ndPtrPtr != NULL; i++) {
|
140
|
+
|
141
|
+
ndPtrPtr = &((*ndPtrPtr)->next);
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
if (i < n) return FAILURE;
|
146
|
+
|
147
|
+
ndPtr = nodeNew(dt, *ndPtrPtr);
|
148
|
+
|
149
|
+
if (ndPtr == NULL) return FAILURE;
|
150
|
+
|
151
|
+
*ndPtrPtr = ndPtr;
|
152
|
+
|
153
|
+
return SUCCESS;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
void insert_data_des(node_t **ndPtrPtr, data_t dt)
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
node_t *new;
|
164
|
+
|
165
|
+
new = nodeNew(dt, NULL);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
int c;
|
170
|
+
|
171
|
+
c = nodeDes(*ndPtrPtr, new);
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
nodeInsert(ndPtrPtr, c, dt);
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
int main()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
node_t *list;
|
186
|
+
|
187
|
+
int i;
|
188
|
+
|
189
|
+
srand((unsigned)time(NULL));
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
for (i = 0; i < N; i++) {
|
194
|
+
|
195
|
+
int r = rand() % 1000;
|
196
|
+
|
197
|
+
insert_data_des(&list, r);
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
listPrint(list);
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
return 0;
|
94
208
|
|
95
209
|
}
|
96
210
|
|
1
試したソースコードを追加しました\.
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,6 +28,74 @@
|
|
28
28
|
|
29
29
|
CELL *head; //head:先頭セルへのポインタ
|
30
30
|
|
31
|
+
|
32
|
+
|
33
|
+
void insert_cell(CELL *c,CELL *p) //セルcをセルpの後に挿入
|
34
|
+
|
35
|
+
{
|
36
|
+
|
37
|
+
c->.next = p->next;
|
38
|
+
|
39
|
+
p->next = c;
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
CELL insert_data_des(int d) //データdを降順で挿入
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
if(head == NULL){
|
50
|
+
|
51
|
+
head->data = d;
|
52
|
+
|
53
|
+
return *head;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
CELL new_cell;
|
60
|
+
|
61
|
+
new_cell.data = d;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
if(new_cell.data > head->data){
|
66
|
+
|
67
|
+
insert_cell(&new_cell, head);
|
68
|
+
|
69
|
+
return new_cell;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
CELL *p;
|
76
|
+
|
77
|
+
p = head->next;
|
78
|
+
|
79
|
+
while(p != NULL){
|
80
|
+
|
81
|
+
if(new_cell.data > p->data){
|
82
|
+
|
83
|
+
insert_cell(&new_cell, p);
|
84
|
+
|
85
|
+
return new_cell;
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
p = p->next;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
return NULL;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
31
99
|
```
|
32
100
|
|
33
101
|
|