C 链表:在列表中插入名称和id号

C 链表:在列表中插入名称和id号,c,insert,linked-list,C,Insert,Linked List,需要帮助在列表中插入姓名和id号。尝试时崩溃 将编号添加到列表中 struct node { int id; char name[50]; struct node *next; } *head; struct node *insert(struct node * list, char nam[50], int id){ struct node *helper; struct node *pNew; pNew=(struct node*)malloc(sizeof(struct node)

需要帮助在列表中插入姓名和id号。尝试时崩溃 将编号添加到列表中

struct node {

int id;
char name[50];
struct node *next;
} *head;


struct node *insert(struct node * list, char nam[50], int id){

struct node *helper;
struct node *pNew;
pNew=(struct node*)malloc(sizeof(struct node));

strcpy(pNew->name, nam);
pNew->id = id;
pNew->next=NULL;

helper=list;

if (list == NULL)
    return pNew;

while (helper->next!=NULL)
    helper=helper->next;

helper->next=pNew;


return list;
}

intmain()
{
字符名称插入[50];
字符名称删除[50];
int-idNum,i;
结构节点*n=NULL;
//链表的开头,必须初始化
head=NULL;
//使用文件输入功能读取数据
readDataFile();
//使用while循环显示数据操作
而(1)
{
printf(“\n数据操作\n”);
printf(“\n----------------\n”);
printf(“1.插入名称\n”);
printf(“2.显示名称\n”);
printf(“3.按ID号删除\n”);
printf(“4.按名称删除\n”);
printf(“5.退出\n”);
printf(“请输入命令:”);

如果(scanf(“%d”,&i)我已简化并修改了您的
insert()
函数

struct node *insert(struct node * list, char *nam, int id){
    struct node *pNew;
    pNew=malloc(sizeof(struct node));
    if (pNew == NULL) {
        printf ("Unable to allocate memory\n")
        exit (1);
    }
    strncpy(pNew->name, nam, 49);       // limit the string copy length
    pNew->name[49] = 0;                 // precautionary terminator
    pNew->id = id;
    pNew->next = list;                  // point to list passed
    return pNew;                        // return new list
}
调用
insert()
的方式会忽略其返回值,即新的列表头/根

n = insert(n, nameInsert, idNum);   // use the return value

请不要强制转换
malloc()
和family的返回值。请向我们显示
pNew->name
是否分配了内存。
name
a
char*
?请向我们显示
结构节点
if(scanf(“%d”,&i)的定义它将我带到正确的情况。它要求我输入名称,然后输入id,然后崩溃。它给我这个错误':(lldb)
n = insert(n, nameInsert, idNum);   // use the return value