【问题标题】:How to implement a linked-list with multiple nodes in C?如何在 C 中实现具有多个节点的链表?
【发布时间】:2015-05-10 21:27:49
【问题描述】:

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

How Many employees? 4

现在,每个人都有firstnamelastnameratezipcode,带有一个链表我正在尝试获取这些输入并根据记录数执行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;}
}

我怎样才能做到这一点?

【问题讨论】:

    标签: c pointers data-structures linked-list


    【解决方案1】:

    要修复的样本

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2020-07-10
      相关资源
      最近更新 更多