2015-05-10 3 views
0

творю связанный список (ов) на основе пользовательского ввода, как следующее:Как реализовать связанный список с несколькими узлами в C?

How Many employees? 4 

Теперь, каждый из которых будет иметь firstnamelastnamerate и zipcode, связанный список я пытаюсь взять эти входы и делает петлю 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;} 
} 

Как я могу получить это право?

ответ

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; 
} 
Смежные вопросы