#1
by ryusei » 6年前
どなたか教えていただけるとありがたいです。
先日から構造体の勉強をしています。
このようなコードをテキストを参考に作成しました。
code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//会員情報の構造体
struct LIST{
unsigned int number;
char name[256];
struct LIST *next;
} ;
int displayList(struct LIST *list){
while (list != NULL){
printf("%d:%s\n", list->number, list->name);
list = list->next;
}
return 0;
}
struct LIST* deleteAllList(struct LIST *list)
{
struct LIST *tmp;
while(list != NULL){
tmp = list->next;
free(list);
list = tmp;
}
return NULL;
}
struct LIST* addList(struct LIST *list, char *name, unsigned int number){
struct LIST *item;
struct LIST *top;
top = list;
item = (struct LIST*)malloc(sizeof(struct LIST));
item->number = number;
strcpy(item->name, name);
item->next = NULL;
//リストが空きの場合先頭に追加して終了
if(list == NULL){
list = item;
return list;
}
while (list != NULL){
list = list->next;
}
list->next = list;
return top;
}
struct LIST* deleteList(struct LIST *list, unsigned int number){
struct LIST *tmp;
struct LIST *top;
top = list;
//先頭ノードだけ別処理
if(list != NULL && list->number == number){
tmp = list->next;
free(list);
return tmp;
}
puts("---");
while(list != NULL){
if(list->next->number == number){
tmp = list->next->next;
free(list->next);
list->next = tmp;
return top;
}
list = list->next;
}
printf("number %d not found!\n", number);
return NULL;
}
int main(void)
{
int operation;
unsigned int inputnumber = 1;
unsigned int number = 1;
char inputname[256];
char buf[100];
int c;
struct LIST *list = NULL;
while (1){
puts("operation(1:add 2;delete etc:exit) = ");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%1d", &operation);
switch (operation){
case 1:
printf("add name = ");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%s", inputname);
buf[strlen(buf) - 1] = '\0';
list = addList(list, inputname, number);
displayList(list);
number++;
break;
case 2:
if(list == NULL){
puts("list is empty\n");
} else {
puts("delete number = ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
sscanf(buf, "%d", &inputnumber);
list = deleteList(list, inputnumber);
}
break;
default:
displayList(list);
list = deleteAllList(list);
displayList(list);
return 0;
}
}
}
/code
ここでおそらく、main関数内のcase(1)文
のfgetsあたりで二回目の入力行おうとすると、
Segmentation fault (コアダンプ)というエラーが出ます。
なぜなのか?ということと解決策を教えていただければ幸いです。
よろしくお願いいたします。
どなたか教えていただけるとありがたいです。
先日から構造体の勉強をしています。
このようなコードをテキストを参考に作成しました。
code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//会員情報の構造体
struct LIST{
unsigned int number;
char name[256];
struct LIST *next;
} ;
int displayList(struct LIST *list){
while (list != NULL){
printf("%d:%s\n", list->number, list->name);
list = list->next;
}
return 0;
}
struct LIST* deleteAllList(struct LIST *list)
{
struct LIST *tmp;
while(list != NULL){
tmp = list->next;
free(list);
list = tmp;
}
return NULL;
}
struct LIST* addList(struct LIST *list, char *name, unsigned int number){
struct LIST *item;
struct LIST *top;
top = list;
item = (struct LIST*)malloc(sizeof(struct LIST));
item->number = number;
strcpy(item->name, name);
item->next = NULL;
//リストが空きの場合先頭に追加して終了
if(list == NULL){
list = item;
return list;
}
while (list != NULL){
list = list->next;
}
list->next = list;
return top;
}
struct LIST* deleteList(struct LIST *list, unsigned int number){
struct LIST *tmp;
struct LIST *top;
top = list;
//先頭ノードだけ別処理
if(list != NULL && list->number == number){
tmp = list->next;
free(list);
return tmp;
}
puts("---");
while(list != NULL){
if(list->next->number == number){
tmp = list->next->next;
free(list->next);
list->next = tmp;
return top;
}
list = list->next;
}
printf("number %d not found!\n", number);
return NULL;
}
int main(void)
{
int operation;
unsigned int inputnumber = 1;
unsigned int number = 1;
char inputname[256];
char buf[100];
int c;
struct LIST *list = NULL;
while (1){
puts("operation(1:add 2;delete etc:exit) = ");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%1d", &operation);
switch (operation){
case 1:
printf("add name = ");
fgets(buf, sizeof(buf), stdin);
sscanf(buf, "%s", inputname);
buf[strlen(buf) - 1] = '\0';
list = addList(list, inputname, number);
displayList(list);
number++;
break;
case 2:
if(list == NULL){
puts("list is empty\n");
} else {
puts("delete number = ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
sscanf(buf, "%d", &inputnumber);
list = deleteList(list, inputnumber);
}
break;
default:
displayList(list);
list = deleteAllList(list);
displayList(list);
return 0;
}
}
}
/code
ここでおそらく、main関数内のcase(1)文
のfgetsあたりで二回目の入力行おうとすると、
Segmentation fault (コアダンプ)というエラーが出ます。
なぜなのか?ということと解決策を教えていただければ幸いです。
よろしくお願いいたします。