我想将新值附加到链表tail,但由于某些原因,似乎没有附加这些值。对于新的链表分配:

struct checkPoints *checkPt = *checkPoint;

while (checkPt != NULL) {
    checkPt = checkPt->next;
}
if (checkPt == NULL) {
    checkPt = malloc(sizeof (struct checkPoints));
    scanf("%c %d %d %d %d\n", &checkPt->dropOut, &checkPt->currentPoint, &checkPt->competitor, &checkPt->hour, &checkPt->minute);
}

思想?

最佳答案

您没有将新项目添加到列表中(并且在分配新项目时也会失去对列表结尾的跟踪)。尝试

struct checkPoints *tail = *checkPoint;

struct checkPoints *newItem = malloc(sizeof (struct checkPoints));
scanf("%c %d %d %d %d\n", &checkPt->dropOut, &checkPt->currentPoint,
                          &checkPt->competitor, &checkPt->hour,
                          &checkPt->minute);
newItem->next = NULL;

if (tail == NULL) {
    *checkPoint = newItem
}
else {
    while (tail->next != NULL) {
        tail = tail->next;
    }
    tail->next = newItem;
}

关于c - 附加到链接列表C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13877478/

10-11 16:08