gpt4 book ai didi

c - 附加到链表 C

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:50 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com