質問編集履歴

3

修正

2022/06/09 03:06

投稿

ambitious
ambitious

スコア0

test CHANGED
File without changes
test CHANGED
@@ -11,125 +11,7 @@
11
11
  実行と同時にセグメンテーション違反となってしまう。
12
12
  ```
13
13
 
14
- ### 該当のソースコード
15
14
 
16
- ```C
17
- #include <stdio.h>
18
- #include <stdlib.h>
19
- struct cell
20
- {
21
- double data;
22
- struct cell *next;
23
- };
24
- void add_list(struct cell **, double);
25
- void show_list(struct cell *);
26
- void free_list(struct cell *);
27
-
28
- int main(void)
29
- {
30
- struct cell **listhead;
31
- *listhead = NULL;
32
- struct cell *p;
33
- double num;
34
- while (1)
35
- {
36
- printf("正の値を入力してください-->");
37
- scanf("%lf", &num);
38
- if (num < 0)
39
- {
40
- show_list(*listhead);
41
- free_list(*listhead);
42
- printf("プログラムを終了します\n");
43
- }
44
- else
45
- {
46
- if ((*listhead) == NULL)
47
- {
48
- p = (struct cell *)malloc(sizeof(struct cell));
49
- *listhead = p;
50
- p->data = num;
51
- p->next = NULL;
52
- }
53
- else if ((*listhead) != NULL && (*listhead)->next == NULL)
54
- {
55
- if ((*listhead)->data > num)
56
- {
57
- p = (struct cell *)malloc(sizeof(struct cell));
58
- p->data = num;
59
- p->next = (*listhead);
60
- *listhead = p;
61
- }
62
- else
63
- {
64
- p = (struct cell *)malloc(sizeof(struct cell));
65
- p->data = num;
66
- p->next = NULL;
67
- p = (*listhead)->next;
68
- }
69
- }
70
- else if (num < (*listhead)->data)
71
- {
72
- p = (struct cell *)malloc(sizeof(struct cell));
73
- p->data = num;
74
- p->next = *listhead;
75
- *listhead = p;
76
- }
77
- else
78
- {
79
- add_list(listhead, num);
80
- show_list(*listhead);
81
- }
82
- }
83
- }
84
- return 0;
85
- }
86
- void add_list(struct cell **head, double n)
87
- {
88
- struct cell *p;
89
- struct cell *q;
90
- q = *head;
91
- if (q->data < n && n < q->next->data)
92
- {
93
- p = (struct cell *)malloc(sizeof(struct cell));
94
- p->data = n;
95
- p->next = q->next;
96
- q->next = p;
97
- }
98
- else if (q->next->next == NULL)
99
- {
100
- p = (struct cell *)malloc(sizeof(struct cell));
101
- p->data = n;
102
- q->next->next = p;
103
- p->next = NULL;
104
- }
105
- else
106
- {
107
- add_list(&(q->next), n);
108
- }
109
- }
110
- void show_list(struct cell *head)
111
- {
112
- struct cell *q = head;
113
- if (q != NULL)
114
- {
115
- printf("-> %lf", q->data);
116
- show_list(q->next);
117
- }
118
- printf("\n");
119
- }
120
-
121
- void free_list(struct cell *head)
122
- {
123
- struct cell *p;
124
- if (head != NULL)
125
- {
126
- p = head;
127
- free(p);
128
- }
129
- free_list(head->next);
130
- }
131
-
132
- ```
133
15
 
134
16
  ### 試したこと
135
17
  少し改変すると、一番目に入力したデータは表示するようになりましたが、二番目以降が表示されなくなります。
@@ -138,6 +20,4 @@
138
20
  ### 補足情報(FW/ツールのバージョンなど)
139
21
  gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
140
22
 
141
- ~~インデントの流儀がよく分かっておらず、見づらいかもしれません。
23
+
142
- ご了承ください~~
143
- インデント出来ました

2

改善

2022/06/09 02:31

投稿

ambitious
ambitious

スコア0

test CHANGED
File without changes
test CHANGED
@@ -16,99 +16,117 @@
16
16
  ```C
17
17
  #include <stdio.h>
18
18
  #include <stdlib.h>
19
-
20
- struct cell{
19
+ struct cell
20
+ {
21
21
  double data;
22
22
  struct cell *next;
23
23
  };
24
- void add_list(struct cell **,double);
24
+ void add_list(struct cell **, double);
25
25
  void show_list(struct cell *);
26
26
  void free_list(struct cell *);
27
27
 
28
- int main(void){
28
+ int main(void)
29
+ {
29
- struct cell **listhead;
30
+ struct cell **listhead;
30
- *listhead=NULL;
31
+ *listhead = NULL;
31
- struct cell *p;
32
+ struct cell *p;
32
- double num;
33
+ double num;
33
- while(1){
34
+ while (1)
35
+ {
34
- printf("正の値を入力してください-->");
36
+ printf("正の値を入力してください-->");
35
- scanf("%lf",&num);
37
+ scanf("%lf", &num);
36
- if(num<0){
38
+ if (num < 0)
39
+ {
37
- show_list(*listhead);
40
+ show_list(*listhead);
38
- free_list(*listhead);
41
+ free_list(*listhead);
39
- printf("プログラムを終了します\n");
42
+ printf("プログラムを終了します\n");
43
+ }
40
- break;
44
+ else
45
+ {
46
+ if ((*listhead) == NULL)
47
+ {
48
+ p = (struct cell *)malloc(sizeof(struct cell));
49
+ *listhead = p;
50
+ p->data = num;
51
+ p->next = NULL;
52
+ }
53
+ else if ((*listhead) != NULL && (*listhead)->next == NULL)
54
+ {
55
+ if ((*listhead)->data > num)
56
+ {
57
+ p = (struct cell *)malloc(sizeof(struct cell));
58
+ p->data = num;
59
+ p->next = (*listhead);
60
+ *listhead = p;
61
+ }
62
+ else
63
+ {
64
+ p = (struct cell *)malloc(sizeof(struct cell));
65
+ p->data = num;
66
+ p->next = NULL;
67
+ p = (*listhead)->next;
68
+ }
69
+ }
70
+ else if (num < (*listhead)->data)
71
+ {
72
+ p = (struct cell *)malloc(sizeof(struct cell));
73
+ p->data = num;
74
+ p->next = *listhead;
75
+ *listhead = p;
76
+ }
77
+ else
78
+ {
79
+ add_list(listhead, num);
80
+ show_list(*listhead);
81
+ }
82
+ }
41
83
  }
42
- else
84
+ return 0;
85
+ }
86
+ void add_list(struct cell **head, double n)
43
87
  {
88
+ struct cell *p;
89
+ struct cell *q;
44
- if((*listhead)==NULL){
90
+ q = *head;
91
+ if (q->data < n && n < q->next->data)
92
+ {
45
- p=(struct cell *)malloc(sizeof(struct cell));
93
+ p = (struct cell *)malloc(sizeof(struct cell));
46
- *listhead=p;
47
- p->data=num;
94
+ p->data = n;
95
+ p->next = q->next;
48
- p->next=NULL;
96
+ q->next = p;
49
- }
97
+ }
50
- else if((*listhead)!=NULL&&(*listhead)->next==NULL){
98
+ else if (q->next->next == NULL)
51
- if((*listhead)->data>num){
99
+ {
52
- p=(struct cell *)malloc(sizeof(struct cell));
100
+ p = (struct cell *)malloc(sizeof(struct cell));
53
- p->data=num;
101
+ p->data = n;
102
+ q->next->next = p;
54
- p->next=(*listhead);
103
+ p->next = NULL;
55
- *listhead=p;
56
- }
104
+ }
57
- else{
105
+ else
58
- p=(struct cell *)malloc(sizeof(struct cell));
59
- p->data=num;
106
+ {
60
- p->next=NULL;
61
- p=(*listhead)->next;
107
+ add_list(&(q->next), n);
62
- }
63
- }else if(num<(*listhead)->data){
64
- p=(struct cell *)malloc(sizeof(struct cell));
65
- p->data=num;
66
- p->next=*listhead;
67
- *listhead=p;
68
- }else{
69
- add_list(listhead,num);
70
- show_list(*listhead);
71
108
  }
72
109
  }
110
+ void show_list(struct cell *head)
111
+ {
112
+ struct cell *q = head;
113
+ if (q != NULL)
114
+ {
115
+ printf("-> %lf", q->data);
73
- }//whileのかっこ
116
+ show_list(q->next);
74
- return 0;
75
- }
117
+ }
76
- void add_list(struct cell **head,double n){
77
- struct cell *p;
78
- struct cell *q;
79
- q=*head;
80
- if(q->data<n&&n<q->next->data){
81
- p=(struct cell *)malloc(sizeof(struct cell));
82
- p->data=n;
83
- p->next=q->next;
84
- q->next=p;
85
- }else if(q->next->next==NULL){
86
- p=(struct cell *)malloc(sizeof(struct cell));
87
- p->data=n;
88
- q->next->next=p;
89
- p->next=NULL;
90
- }
91
- else{
92
- add_list(&(q->next),n);
118
+ printf("\n");
93
119
  }
94
120
 
95
- }
96
- void show_list(struct cell *head){
121
+ void free_list(struct cell *head)
122
+ {
97
- struct cell *q=head;
123
+ struct cell *p;
98
- if(q!=NULL){
124
+ if (head != NULL)
125
+ {
99
- printf("-> %lf",q->data);
126
+ p = head;
100
- show_list(q->next);
127
+ free(p);
101
128
  }
102
- printf("\n");
103
- }
104
-
105
- void free_list(struct cell *head){
106
- struct cell *p;
107
- if(head!=NULL){
108
- p=head;
109
- free(p);
110
- }
111
- free_list(head->next);
129
+ free_list(head->next);
112
130
  }
113
131
 
114
132
  ```
@@ -120,5 +138,6 @@
120
138
  ### 補足情報(FW/ツールのバージョンなど)
121
139
  gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
122
140
 
123
- インデントの流儀がよく分かっておらず、見づらいかもしれません。
141
+ ~~インデントの流儀がよく分かっておらず、見づらいかもしれません。
124
- ご了承ください
142
+ ご了承ください~~
143
+ インデント出来ました

1

修正

2022/06/09 02:05

投稿

ambitious
ambitious

スコア0

test CHANGED
File without changes
test CHANGED
@@ -121,6 +121,4 @@
121
121
  gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
122
122
 
123
123
  インデントの流儀がよく分かっておらず、見づらいかもしれません。
124
- ご了承ください```ここに言語を入力
124
+ ご了承ください
125
- コード
126
- ```