Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链接列表插入中的头始终为空_C_Linked List - Fatal编程技术网

C 链接列表插入中的头始终为空

C 链接列表插入中的头始终为空,c,linked-list,C,Linked List,我正在尝试插入到我的链接列表中。以下是我为同样的目的所写的: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void show_menu() { printf("\nWhat do you want to do: \n"); printf("1.Insert \n2.Delete \n3.Show");

我正在尝试插入到我的链接列表中。以下是我为同样的目的所写的:

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


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


void show_menu()
{
    printf("\nWhat do you want to do: \n");
    printf("1.Insert \n2.Delete \n3.Show"); 
    printf("\nEnter your choice: \n");
}

void insert(struct node *head)
{
    int new_numer;
    printf("Enter a number to insert in the linked list: \n");
    scanf("%d",&new_numer);

    if ( head == NULL )
    {
        head = (struct node *)malloc(sizeof(struct node));
        head->data = new_numer;
        head->next = NULL;
    }
    else
    {
        struct node *temp = head;

        struct node *new_node = (struct node *)malloc(sizeof(struct node));

        new_node -> data = new_numer;

        new_node -> next = NULL;

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


void show(struct node *head)
{
    printf("\n The elements in the list are: \n");
    while ( head != NULL )
    {
        printf("%d\n", head->data);
        head = head -> next;
    }
}


int main(int argc, char const *argv[])
{
    int choice;

    struct node *head = NULL;


    while(1)
    {
        show_menu();
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert(head);
                show(head);
            break;

            case 2:
                show(head);
            break;

            case 3:
            break;

            default:
                printf("Don't fuck with me.\n");
        }

    }


    return 0;
}
为什么列表中没有插入元素

如果我要搬家

head = (struct node *)malloc(sizeof(struct node));
对于主函数,我将第一个元素作为零,另一个元素插入


我在这里遗漏了什么?

您面临的问题是,当您插入第一个元素时,头部没有改变。您已将其值传递给函数


您需要做的是传递head的地址,即
结构节点**head
,然后修改
*head
,如果它是插入的第一个元素。

每当您想要更改传递给函数的某个变量的值时,您需要通过引用传递或使用该变量的指针。这里的情况也是如此。函数
insert
更改变量
head
的值,该变量是指针。所以你必须传递一个指针的指针


因此,参数必须是
struct node**head
,并且在
main()
函数中将
&head
传递到
insert()
是否要附加分配给head的新_节点?
insert()
无法从
main()
修改
head
,因为它是通过值传递的。您需要传递一个指向它的指针(即指向结构节点的指针)可能的重复。您可以详细说明一下吗?因此,您在main中将head设置为NULL。然后将其传递给insert函数。您正在传递一份head。所以函数中的头是空的。现在在if条件下更改它。head的本地副本已更改,但main中的head仍然相同。对于它,实际上没有发生插入。是的,它是指向结构的指针。但这仍然是一个变量。要更改insert函数中变量的内容,需要指向该指针的指针。因此,
struct节点**head
感谢您的帮助。任何相同的教程。这很令人困惑,感觉像是《盗梦空间》:)是的,指针可以有任何深度。对于进一步的解释,您可能希望看到Johnnymapp的评论。
head = (struct node *)malloc(sizeof(struct node));