teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2022/06/09 03:06

投稿

ambitious
ambitious

スコア0

title CHANGED
File without changes
body CHANGED
@@ -11,126 +11,8 @@
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
15
 
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
-
134
16
  ### 試したこと
135
17
  少し改変すると、一番目に入力したデータは表示するようになりましたが、二番目以降が表示されなくなります。
136
18
 
@@ -138,6 +20,3 @@
138
20
  ### 補足情報(FW/ツールのバージョンなど)
139
21
  gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
140
22
 
141
- ~~インデントの流儀がよく分かっておらず、見づらいかもしれません。
142
- ご了承ください~~
143
- インデント出来ました

2

改善

2022/06/09 02:31

投稿

ambitious
ambitious

スコア0

title CHANGED
File without changes
body CHANGED
@@ -16,100 +16,118 @@
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
- struct cell **listhead;
30
- *listhead=NULL;
31
- struct cell *p;
32
- double num;
33
- while(1){
34
- printf("正の値を入力してください-->");
35
- scanf("%lf",&num);
36
- if(num<0){
37
- show_list(*listhead);
38
- free_list(*listhead);
39
- printf("プログラムを終了します\n");
40
- break;
41
- }
42
- else
43
29
  {
30
+ struct cell **listhead;
44
- if((*listhead)==NULL){
31
+ *listhead = NULL;
32
+ struct cell *p;
33
+ double num;
34
+ while (1)
35
+ {
45
- p=(struct cell *)malloc(sizeof(struct cell));
36
+ printf("正の値を入力してください-->");
37
+ scanf("%lf", &num);
38
+ if (num < 0)
39
+ {
46
- *listhead=p;
40
+ show_list(*listhead);
47
- p->data=num;
48
- p->next=NULL;
41
+ free_list(*listhead);
42
+ printf("プログラムを終了します\n");
49
- }
43
+ }
50
- else if((*listhead)!=NULL&&(*listhead)->next==NULL){
44
+ else
45
+ {
51
- if((*listhead)->data>num){
46
+ if ((*listhead) == NULL)
47
+ {
52
- p=(struct cell *)malloc(sizeof(struct cell));
48
+ p = (struct cell *)malloc(sizeof(struct cell));
49
+ *listhead = p;
53
- p->data=num;
50
+ p->data = num;
54
- p->next=(*listhead);
51
+ p->next = NULL;
55
- *listhead=p;
56
- }
52
+ }
53
+ else if ((*listhead) != NULL && (*listhead)->next == NULL)
57
- else{
54
+ {
55
+ if ((*listhead)->data > num)
56
+ {
58
- p=(struct cell *)malloc(sizeof(struct cell));
57
+ p = (struct cell *)malloc(sizeof(struct cell));
59
- p->data=num;
58
+ p->data = num;
60
- p->next=NULL;
59
+ p->next = (*listhead);
61
- p=(*listhead)->next;
60
+ *listhead = p;
62
- }
61
+ }
63
- }else if(num<(*listhead)->data){
62
+ else
63
+ {
64
- p=(struct cell *)malloc(sizeof(struct cell));
64
+ p = (struct cell *)malloc(sizeof(struct cell));
65
- p->data=num;
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;
66
- p->next=*listhead;
74
+ p->next = *listhead;
67
- *listhead=p;
75
+ *listhead = p;
76
+ }
68
- }else{
77
+ else
78
+ {
69
- add_list(listhead,num);
79
+ add_list(listhead, num);
70
- show_list(*listhead);
80
+ show_list(*listhead);
81
+ }
82
+ }
71
83
  }
84
+ return 0;
72
85
  }
73
- }//whileのかっこ
74
- return 0;
75
- }
76
- void add_list(struct cell **head,double n){
86
+ void add_list(struct cell **head, double n)
87
+ {
77
88
  struct cell *p;
78
89
  struct cell *q;
79
- q=*head;
90
+ q = *head;
80
- if(q->data<n&&n<q->next->data){
91
+ if (q->data < n && n < q->next->data)
92
+ {
81
- p=(struct cell *)malloc(sizeof(struct cell));
93
+ p = (struct cell *)malloc(sizeof(struct cell));
82
- p->data=n;
94
+ p->data = n;
83
- p->next=q->next;
95
+ p->next = q->next;
84
- q->next=p;
96
+ q->next = p;
97
+ }
85
- }else if(q->next->next==NULL){
98
+ else if (q->next->next == NULL)
99
+ {
86
- p=(struct cell *)malloc(sizeof(struct cell));
100
+ p = (struct cell *)malloc(sizeof(struct cell));
87
- p->data=n;
101
+ p->data = n;
88
- q->next->next=p;
102
+ q->next->next = p;
89
- p->next=NULL;
103
+ p->next = NULL;
104
+ }
105
+ else
106
+ {
107
+ add_list(&(q->next), n);
108
+ }
90
109
  }
91
- else{
92
- add_list(&(q->next),n);
93
- }
94
-
95
- }
96
- void show_list(struct cell *head){
110
+ void show_list(struct cell *head)
111
+ {
97
- struct cell *q=head;
112
+ struct cell *q = head;
98
- if(q!=NULL){
113
+ if (q != NULL)
114
+ {
99
- printf("-> %lf",q->data);
115
+ printf("-> %lf", q->data);
100
116
  show_list(q->next);
101
117
  }
102
- printf("\n");
118
+ printf("\n");
103
119
  }
104
120
 
105
- void free_list(struct cell *head){
121
+ void free_list(struct cell *head)
122
+ {
106
- struct cell *p;
123
+ struct cell *p;
107
- if(head!=NULL){
124
+ if (head != NULL)
125
+ {
108
- p=head;
126
+ p = head;
109
- free(p);
127
+ free(p);
128
+ }
129
+ free_list(head->next);
110
130
  }
111
- free_list(head->next);
112
- }
113
131
 
114
132
  ```
115
133
 
@@ -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

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