リンクリスト付きのトランプを作成するプログラムを作成しようとしています。私の挿入機能は、入力されたカード番号とそのスーツをリストの最後に追加する必要があります。私が作成したコードは、すでにセグメンテーション違反を引き起こしています。
#include <stdio.h>
#include <string.h>
struct node{
char number;
char suit;
struct node *next;
};
int insert(struct node *s, char su, char num)
{
struct node *temp=s;
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->number=num;
newnode->suit=su;
while(temp->next)
{
temp=temp->next;
temp->next=newnode;
}
return 0;
}
main()
{
struct node *head;
char x;
while(1)
{
printf("What would you like to do?\n");
printf("Insert: i\n");
scanf("%s",&x);
if(x=='i')
{
char su, num;
printf("Please enter the suit\n");
scanf("%s",&su);
printf("Please enter the number\n");
scanf("%s",&num);
insert(head, su,num);
}
}
}