我创建了我的结构的链表,但由于某种原因,每次我添加另一个链接时它都会改变头地址,但我希望 y 头地址是第一个条目。这是我的代码:
struct checkPoints *tgh = NULL;
struct checkPoints **linkedlist = &tgh;
struct checkPoints *cp = malloc(sizeof (struct checkPoints));
chPo = fopen(fileName, mode);
if (chPo == NULL) {
printf("Can't find the files.");
exit(1);
} else {
for (i = 0; i < lines; i++) {
fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
cp->next = NULL;
if (*linkedlist == NULL) {
printf("ONCE");
*linkedlist = cp;
} else {
struct checkPoints *new = *linkedlist;
while (new->next != NULL) {
new = new->next;
}
new->next = cp;
}
}
}
每个 fscanf 发生它都会将头地址更改为下一个,有什么想法吗?
此行之后的头地址更改:fscanf(chPo, "%c %d %d %d:%d\n", &cp->dropOut, &cp->currentPoint, &cp->competitor, &cp->hour, &cp->minute);
结构是这样的:
struct checkPoints{
char dropOut;
int currentPoint;
int competitor;
int hour;
int minute;
struct checkPoints *next;
};