在c中添加到链表的末尾

在c中添加到链表的末尾,c,insert,linked-list,singly-linked-list,C,Insert,Linked List,Singly Linked List,我正试图编写一个程序,用一个链表创建一副卡片。我的插入功能需要将输入的卡号及其套装添加到列表的末尾。我创建的代码已经给了我一个分段错误 #include <stdio.h> #include <string.h> struct node{ char number; char suit; struct node *next; }; int insert(struct node *s, char su, char num) { str

我正试图编写一个程序,用一个链表创建一副卡片。我的插入功能需要将输入的卡号及其套装添加到列表的末尾。我创建的代码已经给了我一个分段错误

#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);
      }
   }
}
#包括
#包括
结构节点{
字符数;
炭服;
结构节点*下一步;
};
int insert(结构节点*s,字符su,字符num)
{
结构节点*temp=s;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
newnode->number=num;
newnode->sut=su;
while(临时->下一步)
{
温度=温度->下一步;
temp->next=newnode;
}
返回0;
}
main()
{
结构节点*头部;
字符x;
而(1)
{
printf(“您想做什么?\n”);
printf(“插入:i\n”);
scanf(“%s”和&x);
如果(x=='i')
{
char su,num;
printf(“请输入套装”\n);
scanf(“%s”和&su);
printf(“请输入数字”);
scanf(“%s”和&num);
插入(头部、su、num);
}
}
}
插入()
temp->next=newnode应该在while循环之外。
还应分配
newnode->next=NULL

另外*头未初始化,最后?你应该分配更多的内存 添加到多个字符的字符串(+可能的填充)

否:
charx,su,num;scanf(“%s”和&su)
是:
const int MAX=100;字符x[MAX],su[MAX],num[MAX];scanf(“%s”,su)

是无限循环,除非传入的指针具有
s->next==NULL
。在第一次迭代中,
temp被移动到
s->next
,然后它的
next
指针被设置为指向
newnode
,因此它不是
NULL
。然后
temp移动到
newnode
,其
next
指针设置为
newnode
--自身,oops

您需要将
newnode
的赋值移到循环之外

while(temp->next)
{
   temp=temp->next;
}
temp->next=newnode;
并初始化
newnode->next=NULL关于构造

当你打电话的时候

insert(head, su,num);

main
中,
head
是一个未初始化的指针,因此您正在调用未定义的行为。

其他答案已经指出了您的错误,但它们留给您的问题是如何让
main
中的
head
指向您创建的列表。有两种方法可以做到这一点。一个是使用双指针,另一个是从
insert
返回指针

在双指针方法中,
insert
将具有原型

int insert(struct node **s, char su, char num); 
struct node *insert(struct node *s, char su, char num); 
并将被调用,地址为
head

struct node *head = NULL; // initialise to NULL, an empty list
... // get the new values for su/num
insert(&head, su, num);
struct node *head = NULL; // initialise to NULL, an empty list
... // get the new values for su/num
head = insert(head, su, num);
这允许
insert
访问head变量的地址并在该变量中设置一个值。这将在列表为空时完成。在
insert
中,您可以执行以下操作:

if (*s == NULL) {
    *s = newnode;
} else {
    // attach to end of list
}
if (s == NULL) {
    s = newnode;
} else {
    // attach to end of list
}
return s;
在指针返回方法中,
insert
将具有原型

int insert(struct node **s, char su, char num); 
struct node *insert(struct node *s, char su, char num); 
并将被调用,地址为
head

struct node *head = NULL; // initialise to NULL, an empty list
... // get the new values for su/num
insert(&head, su, num);
struct node *head = NULL; // initialise to NULL, an empty list
... // get the new values for su/num
head = insert(head, su, num);
在这个方法中,
insert
返回一个指向列表头的指针;当列表为空时,它返回newnode。在
insert
中,您可以执行以下操作:

if (*s == NULL) {
    *s = newnode;
} else {
    // attach to end of list
}
if (s == NULL) {
    s = newnode;
} else {
    // attach to end of list
}
return s;

编译器会惩罚您,因为它看到您正在强制转换
malloc()
的返回值。此代码甚至不可能在不出现极有可能的seg错误的情况下通过第一个scanf()。你要担心的事情远比把一个项目附加到一个链表上要糟糕得多;未初始化指针、将字符串读入单个字符、程序退出时内存泄漏、库头不正确/丢失等。在尝试进行链表管理之前,您需要一个C语言的高级复习课程。很明显,它从未见过调试器,如果真的出现,它可能会在《盗梦空间》中崩溃。@H2CO3还没有读到那么多。