Data structures 在C+中的linkedlist末尾插入节点+;

Data structures 在C+中的linkedlist末尾插入节点+;,data-structures,linked-list,Data Structures,Linked List,下面是在链表末尾插入节点的代码。我遇到了一个分割错误。请帮忙 Node* Insert(Node *head,int data) { struct Node *last=head; struct Node* n=(struct Node*)malloc(sizeof(struct Node)); n->data=data; n->next=NULL; if(head==NULL) { head=n; }

下面是在链表末尾插入节点的代码。我遇到了一个分割错误。请帮忙

Node* Insert(Node *head,int data)
{
    struct Node *last=head;

    struct Node* n=(struct Node*)malloc(sizeof(struct Node));


    n->data=data;
    n->next=NULL;

    if(head==NULL)
    {
        head=n;
    }

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

    last->next=n;
    return 0;
}

您的函数正在返回数值文本0,但其返回类型为“Node*”,因此,一旦尝试使用函数的返回值,您就会遇到麻烦。

你打算回来干什么?修改名单的头头是谁?还是新添加的节点?无论哪种方式,它都不会返回它们。

我想返回modifiedd链接列表的头。不过,您已经将链接列表的头传递给了函数,因此将其作为函数的返回值将不会获得任何有用的结果。但是,如果您想返回它,那么它很简单:
returnhead如果调用函数时head为NULL,则返回新节点,否则返回函数的第一个参数。