C在链表末端插入节点



我在c中插入链表的方法遇到了一些麻烦。它似乎只在列表的开头添加。其他插入都失败了。这个CodeBlocks调试器是如此难以理解,我仍然不明白。它从来不给我值,只给我内存中的地址。不管怎样,这是我的函数。你知道它失败的原因吗?

/* 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 noden");
        exit(-1);
    }
    newNode->value = val;
    //check for first insertion
    if(head->next == NULL){
        head->next = newNode;
        printf("added at beginningn");
    }
    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 latern");
            }
            current = current->next;
        }
    }
    return 0;
}

在main中,只添加了929。

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

这段代码可以工作。样本偏差的答案几乎是正确的,但你需要第三个改变:

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 noden");
        exit(-1);
    }
    newNode->value = val;
    newNode->next = NULL;  // Change 1
    //check for first insertion
    if(head->next == NULL){
        head->next = newNode;
        printf("added at beginningn");
    }
    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 latern");
                break; // Change 3
            }
            current = current->next;
        };
    }
    return 0;
}

修改1:newNode->next必须设置为NULL,这样我们就不会在列表的末尾插入无效指针。

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

编辑:关于while循环,这种方式要好得多:

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

设置mallocnode后,请确保设置node->next = NULL

int addNodeBottom(int val, node *head)
{    
    node *current = head;
    node *newNode = (node *) malloc(sizeof(node));
    if (newNode == NULL) {
        printf("malloc failedn");
        exit(-1);
    }    
    newNode->value = val;
    newNode->next = NULL;
    while (current->next) {
        current = current->next;
    }    
    current->next = newNode;
    return 0;
}    

我应该指出,在这个版本中,head仍然用作dummy,而不是用于存储值。这允许您通过仅使用head节点来表示空列表。

我知道这是一个旧的帖子,但只是供参考。下面是如何在没有对空列表进行特殊情况检查的情况下进行追加,尽管这会牺牲看起来更复杂的代码。

void Append(List * l, Node * n)
{
    Node ** next = &list->Head;
    while (*next != NULL) next = &(*next)->Next;
    *next = n;
    n->Next = NULL;
}

我想在编写代码之前提到这个键,以供您考虑。

//关键

temp= malloc函数分配的新节点的地址(成员id为C中的alloc.h库)

prev=现有链表最后一个节点的地址。

next =包含下一个节点的地址

struct node {
    int data;
    struct node *next;
} *head;
void addnode_end(int a) {
    struct node *temp, *prev;
    temp = (struct node*) malloc(sizeof(node));
    if (temp == NULL) {
        cout << "Not enough memory";
    } else {
        node->data = a;
        node->next = NULL;
        prev = head;
        while (prev->next != NULL) {
            prev = prev->next;
        }
        prev->next = temp;
    }
}

新节点总是添加在给定链表的最后一个节点之后。例如,如果给定的链表是5->10->15->20->25,我们在末尾添加一个项目30,那么链表就变成5->10->15->20->25->30。由于链表通常由它的头表示,因此我们必须遍历列表直到末尾,然后将最后一个节点的下一个节点更改为新节点。

/* Given a reference (pointer to pointer) to the head
   of a list and an int, appends a new node at the end  */

    void append(struct node** head_ref, int new_data)
    {
    /* 1. allocate node */
         struct node* new_node = (struct node*) malloc(sizeof(struct node));
        struct node *last = *head_ref;  /* used in step 5*/
    /* 2. put in the data  */
        new_node->data  = new_data;
    /* 3. This new node is going to be the last node, so make next 
          of it as NULL*/
        new_node->next = NULL;
    /* 4. If the <a href="#">Linked List</a> is empty, then make the new node as head */
        if (*head_ref == NULL)
        {
       *head_ref = new_node;
       return;
        }  
    /* 5. Else traverse till the last node */
        while (last->next != NULL)
        last = last->next;
    /* 6. Change the next of last node */
        last->next = new_node;
        return;    
}

运行正常:

struct node *addNode(node *head, int value) {
    node *newNode = (node *) malloc(sizeof(node));
    newNode->value = value;
    newNode->next = NULL;
    if (head == NULL) {
        // Add at the beginning
        head = newNode;
    } else {
        node *current = head;
        while (current->next != NULL) {
            current = current->next;
        };
        // Add at the end
        current->next = newNode;
    }
    return head;
}

使用例子:

struct node *head = NULL;
for (int currentIndex = 1; currentIndex < 10; currentIndex++) {
    head = addNode(head, currentIndex);
}

相关内容

  • 没有找到相关文章

最新更新