gpt4 book ai didi

c - 如何用C实现多节点链表?

转载 作者:行者123 更新时间:2023-11-30 15:20:09 25 4
gpt4 key购买 nike

我正在根据用户输入创建链接列表,如下所示:

How Many employees? 4

现在,每个人都会有 firstname lastname ratezipcode,以及我正在尝试的链接列表获取这些输入并根据记录数执行 for 循环,但显然我做得不对:

    struct records {
char first[20];
char last[20];
float rate;
int zip;
struct node* next;
};
void main()
{
int i,n;
printf("Please indicate the number of records : ");
scanf("%d",&n);
struct records *head,*conductor;
head=(struct records*)malloc(n*sizeof(struct records));
head->next=NULL;
for (i=0;i<n;i++){
printf("\nEnter employee information in the format :\nFirstname Lastname rate Zipcode (newline for the next)\n");
scanf("%s %s %f %d",&head->first,&head->last,&head->rate,&head->zip);
conductor=head;
conductor=conductor->next;}
}

我怎样才能做到这一点?

最佳答案

要修复的示例

struct records {
char first[20];
char last[20];
float rate;
int zip;
struct records *next;//typo struct node* next;
};
int main(void){//return type is `int`
int i, n;
printf("Please indicate the number of records : ");
scanf("%d", &n);

struct records *head,*conductor;
head=(struct records*)malloc(n*sizeof(struct records));
for (i=0; i<n; i++){
printf("\nEnter employee information in the format :\nFirstname Lastname rate Zipcode (newline for the next)\n");
scanf("%19s %19s %f %d", head[i].first, head[i].last, &head[i].rate, &head[i].zip);
head[i].next = &head[i+1];
}
head[n-1].next = NULL;

return 0;
}

关于c - 如何用C实现多节点链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com