【问题标题】:Adding a node at the beginning of a Singly Linked List (C)在单链表的开头添加一个节点 (C)
【发布时间】:2020-05-18 10:38:03
【问题描述】:

代码可以正常工作,但我似乎不知道为什么新节点没有插入到列表的开头。它可能与第一个函数 (insertNode) 中的 else 语句有关,但我不确定,这是怎么回事?

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *link;
};

void insertNode(struct node *head, int x) {
    //Create node to be added and add the input integer to it
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = x;

    //Check if there are any existing nodes in the list, if not, the let the head be equal to the new temp pointer
    if (head == NULL) {
        head = temp;
    } else {
        //If not, then we need to add the node to the beginning
        temp->link = head;
        head = temp;        
        printf("Node was added successfully!\n");
    }
}

int findsize(struct node *head) {
    //Finds the size of the list

    struct node *temp = head;
    int count = 0;
    while (temp != NULL) {
        count++;
        temp = temp->link;
    }
    return count;
}

void printData(struct node *head) {
    //Prints the elements of the list
    struct node *temp = head;
    while (temp != NULL) {
        printf("Element: %d\n", temp->data);
        temp = temp->link;
    }
}

void main() {
    //Created a node and allocated memory
    struct node *head;
    head = (struct node *)malloc(sizeof(struct node));
    //Added data to the node and created another one linked to it
    head->data = 15;
    head->link = (struct node *)malloc(sizeof(struct node));
    head->link->data = 30;
    head->link->link = NULL;
    //Used the above function to add a new node at the beginning of the list
    insertNode(head, 5);
    //Print the size of the list    
    printf("The size of the list you gave is: %d\n", findsize(head));
    //Print the elements of the list
    printData(head);
}

【问题讨论】:

  • “添加一个节点”意味着改变一些指针,要么是另一个节点中的 ->next 指针,要么是 (externalglobal) 头指针.

标签: c singly-linked-list


【解决方案1】:

当你在列表的开头插入一个节点时,你实际上改变了列表的开头,所以这个新的初始节点必须返回给调用者。 insertNode() 的原型必须更改为返回列表头或获取指向列表头的指针。

这是第一种方法的修改版本:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *link;
};

struct node *insertNode(struct node *head, int x) {
    //Create node to be added and add the input integer to it
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    if (temp != NULL) {
        temp->data = x;
        temp->link = head;
    }
    return temp;
}

int findsize(struct node *head) {
    //Find the size of the list
    struct node *temp = head;
    int count = 0;
    while (temp != NULL) {
        count++;
        temp = temp->link;
    }
    return count;
}

void printData(struct node *head) {
    //Prints the elements of the list
    struct node *temp = head;
    while (temp != NULL) {
        printf("Element: %d\n", temp->data);
        temp = temp->link;
    }
}

void main() {
    //Created a node and allocated memory
    struct node *head = NULL;
    //Insert 3 nodes with values 30, 15 and 5
    head = insertNode(head, 30);
    head = insertNode(head, 15);
    head = insertNode(head, 5);
    //Print the size of the list    
    printf("The size of the list you gave is: %d\n", findsize(head));
    //Print the elements of the list
    printData(head);
    //Should free the nodes
    return 0;
}

【讨论】:

  • 感谢您的宝贵时间。但是,我看不到这两个函数(你的和我的)之间最终结果的差异。我完全理解你的功能,但不是你所做的类似于改变 head 的值(写 head = temp(在我的函数中与写 head = insertNode(head, 5); 在你的)。即 head 是否恢复为我退出函数后它的原始位置?
  • @Ali:我的函数更简单,最重要的是 返回head 的新值给调用者,它被存储到自己的列表指针中:@987654324 @。您的函数没有将新的头返回给调用者,导致列表在main 函数中保持不变,因为head 仍然指向前一个列表头。
  • 您说“...因为 head 仍然指向上一个列表头”。我意识到我可能在这里遗漏了一些重要的东西(我还在学习这个),但没有(head=temp;)改变头指针的位置,使它指向新节点,因为 temp 指向新节点?
  • @Ali:head = temp只更新函数insertNode的参数,而不是main中的同名局部变量。当您在 main() 中调用 insertNode(head, 5); 时,head 值的副本将作为参数传递给函数,顺便说一下,在 insertNode 的定义中可以以不同的方式命名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多