C链表在末尾插入节点 [英] C linked list inserting node at the end

查看:23
本文介绍了C链表在末尾插入节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C 中插入链表的方法遇到了一些问题.它似乎只在列表的开头添加.我所做的任何其他插入都失败了.这个 CodeBlocks 调试器太难理解了,我还是不明白.它从来没有给我价值,只是内存中的地址.无论如何,这是我的职责.你看到它失败的任何原因了吗?

I'm having some trouble with my insertion method for a linked list in C. It seems to only add at the beginning of the list. Any other insertion I make fail. And this CodeBlocks debugger is so hard to understand I still don't get it. It never gives me value, just addresses in memory. Anyway this is my function. Do you see any reason why it's failing?

/* function to add a new node at the end of the list */
int addNodeBottom(int val, node *head){

    //create new node
    node *newNode = (node*)malloc(sizeof(node));

    if(newNode == NULL){
        fprintf(stderr, "Unable to allocate memory for new node
");
        exit(-1);
    }

    newNode->value = val;

    //check for first insertion
    if(head->next == NULL){
        head->next = newNode;
        printf("added at beginning
");
    }

    else
    {
        //else loop through the list and find the last
        //node, insert next to it
        node *current = head;
        while(current->next != NULL)
        {
            if(current->next == NULL)
            {
                current->next = newNode;
                printf("added later
");
            }
            current = current->next;
        }
    }
    return 0;
}

然后在 main 中,只添加了 929.

Then in main, only 929 is added.

   //testing addNodeBottom function
    addNodeBottom(929, head);
    addNodeBottom(98, head);
    addNodeBottom(122, head);
    addNodeBottom(11, head);
    addNodeBottom(1034, head);

推荐答案

此代码将起作用.samplebias 的答案几乎是正确的,但您需要进行第三次更改:

This code will work. The answer from samplebias is almost correct, but you need a third change:

int addNodeBottom(int val, node *head){

    //create new node
    node *newNode = (node*)malloc(sizeof(node));

    if(newNode == NULL){
        fprintf(stderr, "Unable to allocate memory for new node
");
        exit(-1);
    }

    newNode->value = val;
    newNode->next = NULL;  // Change 1

    //check for first insertion
    if(head->next == NULL){
        head->next = newNode;
        printf("added at beginning
");
    }

    else
    {
        //else loop through the list and find the last
        //node, insert next to it
        node *current = head;
        while (true) { // Change 2
            if(current->next == NULL)
            {
                current->next = newNode;
                printf("added later
");
                break; // Change 3
            }
            current = current->next;
        };
    }
    return 0;
}

更改 1:newNode->next 必须设置为 NULL 以便我们不会在列表末尾插入无效指针.

Change 1: newNode->next must be set to NULL so we don't insert invalid pointers at the end of the list.

Change 2/3:将循环改为无限循环,当我们找到最后一个元素时会用 break; 跳出.注意 while(current->next != NULL) 之前是如何与 if(current->next == NULL) 矛盾的.

Change 2/3: The loop is changed to an endless loop that will be jumped out with break; when we found the last element. Note how while(current->next != NULL) contradicted if(current->next == NULL) before.

关于while循环,这样更好:

Regarding the while loop, this way it is much better:

  node *current = head;
  while (current->next != NULL) {
    current = current->next;
  }
  current->next = newNode;
  printf("added later
");

这篇关于C链表在末尾插入节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆