向后添加一个链接列表

时间:2015-01-07 09:37:10

标签: c linked-list nodes

我试图将元素添加到链接列表的后面。我尝试通过遍历列表来实现这一点,直到我将指针指向0。然后我创建一个指向此指针的新指针,并尝试让它指向我列表中的新节点。编译器没有备注,但是当我尝试写下我的列表时,它不包含我试图添加的元素。

void add_back(Node * s, int x) {
    Node * new_node = malloc(sizeof(Node));
    Node * start = s;
    new_node->value = x;
    new_node->next = 0;
    while(start != 0) {
        start = start->next;
    }
    Node ** plaats = &start;
    *plaats = new_node;
}

使用过的结构:

struct Node {
    int value;
    struct Node * next;
};
typedef struct Node Node;

5 个答案:

答案 0 :(得分:3)

你做了很难 - 这就是你在最后几行中所需要做的一切。

void add_back(Node * s, int x) {

    if(s == NULL)  // handle empty list
            return;

    Node * new_node = malloc(sizeof(Node));
    new_node->value = x;
    Node * start = s;
    while(start->next != NULL) { //reach the last node - don't traverse further
        start = start->next;
    }
    new_node->next = NULL;
    start->next = newnode;

    /* not required
    Node ** plaats = &start;
   *plaats = new_node;
    */
}

此:

    while(start->next != 0) { //reach the last node - don't traverse further
        start = start->next;
    }

让你到达这里:

 +----+-----+   +-----+-----+   +-----+------+
 |    |     +-->|     |     +-->|     | NULL |
 +----+-----+   +-----+-----+   +-----+------+
                                /
                            LastNode 

这两行:

    new_node->next = 0;
    start->next = newnode;

这样做:

 +----+-----+   +-----+-----+   +-----+------+           +-----+------+
 |    |     +-->|     |     +-->|     |    ------------->|     | NULL |
 +----+-----+   +-----+-----+   +-----+------+           +-----+------+
                                                          /
                                                       New Node 

答案 1 :(得分:1)

当列表为空时,该函数可以更改列表的头部。所以你必须通过引用传递头部。

因此该功能将采用以下方式

void add_back( Node **head, int x ) 
{
    Node **tail = head;
    Node *new_node;

    while ( *tail != NULL ) tail = &( *tail )->next;

    new_node = malloc( sizeof( Node ) );
    new_node->value = x;
    new_node->next = NULL;

    *tail = new_node;
}

因此,如果您要定义列表

Node *head = NULL;

然后调用该函数,如

int i = 0;
for ( ; i < 10; i++ ) add_back( &head, i ); 

答案 2 :(得分:0)

问题是找到最后一个节点的循环,当循环结束时start不是最后一个节点,它是NULL。然后,您将获得start的地址,该地址将为您提供指向本地变量start的指针,并将新节点分配给该位置。

而是检查列表是否为空,然后将节点添加为第一个节点。如果列表不为空,则在start->next不是NULL时循环,并使start->next指向新节点。

答案 3 :(得分:0)

另外,我建议增强函数add_back()以获取节点列表头的POINTER,并检查头是否为空并产生新头,否则执行建议的解决方案

Node *add(Node **s, int x)
{
  Node *n = malloc(sizeof(Node));
  ... fill n;
  n->next = NULL;

  /* no head parameter */
  if (s == NULL) {
    return n;
  }

  /* empty head */
  if (*s == NULL) {
    *s = n;
  } else {
  /* append to end */
     ... code from other solution with (*s) instead of s
  }

  return *s;
}


...
Node *head = NULL;
add(&head, 1);
...

通过返回may-new head,你可以使用add()作为函数并立即使用结果

head = NULL; 
printnodelist(add(&head, 1));
iterateovernodelist(head);

答案 4 :(得分:0)

 void add_back(Node * s, int x) {

一开始就错了。使用该原型,您无法添加到空列表中。