附加到链表C.

时间:2012-12-14 11:09:11

标签: c linked-list

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

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

想法?

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

你是一个人。

你循环直到checkPtNULL,但此时你已经忘记了列表的 last 元素(checkPt < em>在之前变为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;
}

答案 2 :(得分:0)

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

将循环,直到checkPt为NULL,因此您不指向最后一个元素。

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

checkPt = malloc(sizeof (struct checkPoints));
lastitem->next = checkPt;
lastitem = checkPt;

答案 3 :(得分:0)

你正在覆盖尾节点内存。

您需要创建临时节点

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

然后将temp节点添加到尾部。

checkPt-&gt; next = checktmp;

答案 4 :(得分:0)

循环直到checkPt NULL

你可以看到:

  

O1 - &gt; O2 - &gt; 03 - &gt; ... - &gt;开 - &gt; 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为null,我的代码将失败:通常在列表中没有任何内容时。所以,这是修改后的代码:

// 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.
}

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