我正在尝试编写一个程序来创建带有链表的一副卡片。我的插入功能需要将输入的卡号及其花色添加到列表的末尾。我创建的代码已经给了我一个分段错误。
#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);
}
}
}