0

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

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);
}

想法?

4

5 回答 5

3

您没有将新项目添加到列表中(并且在分配新项目时也会忘记列表的末尾)。尝试

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;
}
于 2012-12-14T11:11:24.517 回答
0

你差一个。

您循环直到checkPtis NULL,但此时您已经忘记了列表的最后一个元素(在它变为checkPt 之前NULL)。

解决方案可能是循环直到该next字段为 NULL:

for( checkPt = *checkPoint;
     checkPt != NULL && checkPt->next != NULL;
     checkPt = checkPt->next)
{
   /* Nothing to do here. */
}

if( checkPt != NULL )
{
  struct checkPoints *np = malloc(sizeof *checkPt->next);
  scanf("%c %d %d %d %d\n", &np->dropOut, &np->currentPoint, &np->competitor, &np->hour, &np->minute);
  np->next = NULL;
  checkPt->next = np;
}
于 2012-12-14T11:12:46.033 回答
0

while (checkPt != NULL) {
    checkPt = checkPt->next;
}

将循环直到 checkPt 为 NULL 所以你没有指向最后一个元素。

最简单的方法是有一个额外的指针指向列表中的最后一个元素,这使得附加元素更有效,而不是总是通过每次附加来寻找它,例如

checkPt = malloc(sizeof (struct checkPoints));
lastitem->next = checkPt;
lastitem = checkPt;
于 2012-12-14T11:14:29.717 回答
0

您正在覆盖尾节点内存。

您需要创建一个临时节点,

checktmp = (struct checkPoints*)malloc(sizeof (struct checkPoints));

然后将临时节点添加到尾部。

checkPt->下一个 = checktmp;

于 2012-12-14T11:15:21.663 回答
0

您循环直到checkPtNULL

你可以看到这个:

O1 --> O2 --> 03 --> ... --> On --> NULL

所以,checkPt是空的,当你创建对象并checkPt指向时,没问题。

问题是:on 现在不是指向 checkPt。它仍然指向NULL。(意思是:什么都没有)。

所以,你应该追踪到最后一个节点(在这种情况下是On),并创建新对象,并使这个 lastNode 指向。

这是我的示例代码:

struct checkPoints *tailPoint = *checkPoint;

  while (tailPoint->next != NULL) {
    tailPoint = tailPoint->next;
  }
  struct checkPoints *newPoints = malloc(sizeof (struct checkPoints));
  // modify newPoints here
  newPoints->next = NULL; // mark it as last node      
  tail->next = newPoint;  // now, mark you last node point to new node. and new node point to end --> NICE

如您所见,我的代码有一个简单的不同点:我跟踪直到tailPoint->next为 NULL,所以我仍然保留最后一个元素,但在您的代码中,什么都没有。这是一个很大的区别。

正如 simonce 评论的那样,如果 checkPoint 为空,我的代码将失败:通常当列表中没有任何内容时。所以,这里是修改后的代码:

// i'm create newPoint first, because always need new points. this will make the code less redundant.

struct checkPoints *tailPoint = *checkPoint;
struct checkPoints *newPoint = malloc(sizeof (struct checkPoints));
// modified newPoint here
// newPoint always last node, so, it will point to NULL.
newPoint->next = NULL; 

if (checkPoint== NULL) {
    *checkPoint = newItem;  // base on new idea
}
else {
    while (tailPoint->next != NULL) {
        tailPoint = tailPoint->next;
    }
    tailPoint->next = newItem;  // as upper code, add last node to tailPoint.
}

您应该注意到两种情况之间的不同:分配newItemcheckPoint(链表的第一个节点)和tailPoint(链表的最后一个节点)希望这有帮助:)

于 2012-12-14T11:19:50.837 回答