我正试图从structs
的array
构建一个Linkedlist
,如下所示:
node * current = NULL, *list= NULL;
current = (node *)malloc(sizeof(node));
current->employee.EMP_ID = ed[0].EMP_ID;
strcpy(current->employee.name, ed[0].name);
current->employee.dept = ed[0].dept;
current->employee.rank = ed[0].rank;
current->employee.salary = ed[0].salary;
current->next = NULL;
list = current;
current = current->next;
for (int i = 1; i < 2; i++){
node *current = (node *)malloc(sizeof(node));
current->employee.EMP_ID = ed[i].EMP_ID;
strcpy(current->employee.name, ed[i].name);
current->employee.dept = ed[i].dept;
current->employee.rank = ed[i].rank;
current->employee.salary = ed[i].salary;
current->next = NULL;
list->next = current;
current = current->next;
}
但是,它只存储struct
中的最后一个值。我能做什么?
尝试这个
node * current = NULL, *list= NULL;
current = (node *)malloc(sizeof(node));
current->employee = ed[0];
current->next = NULL;
list = current;
for (int i = 1; i < 2; i++){
node *np = (node *)malloc(sizeof(node));
np->employee = ed[i];
np->next = NULL;
current = current->next = np;
}