简体   繁体   English

无法工作的单个链表C

[英]Not working single linked list C

I am too bad now. 我现在太糟了。 My list is not working! 我的清单不起作用! I know that there is an issue of just coping my ptr into function, not actually using real one, but I can't understand how can I make this work as I want. 我知道存在一个问题,就是仅将我的ptr转换为功能,而不是实际使用真正的ptr,但是我无法理解如何根据需要进行这项工作。

PS. PS。 I see also that if I make head as global value, it will be ok. 我还看到,如果我以全球价值为先,那没关系。 But I want to get function, which I can call it specific list. 但是我想获得功能,可以将其称为特定列表。

Here is function of adding element into a blamk list. 这是将元素添加到空白列表的功能。 I can't make even this function work. 我什至无法使此功能正常工作。 I tried to play with double pointers, but now I am here to ask some help. 我尝试过使用双指针,但是现在我在这里寻求帮助。

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

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

void add( int num,struct node * head )
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
}

int  main()
{
    struct node *head;
    head=NULL;
    add(20,head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}

UPD: MY own playing with double pointers: UPD:我自己使用双指针玩:

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

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

void add( int num, struct node **head )
{
    struct node **temp;
    temp=(struct node **)malloc(sizeof(struct node*));
    (*temp)->data=num;
    if (*head== NULL)
    {
    *head=*temp;
    (*head)->next=NULL;
    }
        else
{
(*temp)->next=head;
*head=temp;
}

}

int  main()
{
    struct node *head;
    head=NULL;
    add(20,&head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}

How do you expect it will work if you don't pass the address of head to the function? 如果不将head地址传递给函数,您如何期望它会起作用?

Correct code: 正确的代码:
add(20,&head)

And you also have to change your function signature with this: 而且,您还必须使用以下命令更改函数签名:
void add(int num, struct node **head)

Also, to refer to your struct node pointer, again in the add function you'll want to change head with *head . 此外,参考你的struct node的指针,再add你要改变功能head*head

Please pay attention: **head points to *head , so everytime you want to apply changes to your pointer (not double pointer ) head , you have to tell it to the compiler by using *head . 请注意: **head指向*head ,因此每次您要对指针(而不是双指针head进行更改时,都必须使用*head将其告知编译器。

How are you expecting it to work? 您期望它如何运作? It wont work unless you pass the pointer head itself, here you are just creating a copy of it. 除非您传递指针head本身,否则它将无法工作,在这里您只是在创建它的副本。 You can do this as well. 您也可以这样做。

head = add(20,head);

Instead of just 不只是

add(20,head);

Add a 添加一个

return head;

before ending the function 在结束功能之前

And don't forget to change the return type of your function like this 并且不要忘记像这样更改函数的返回类型

struct node* add( int num,struct node *head)

The updated code looks like this 更新后的代码如下所示

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

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

struct node* add( int num,struct node *head)
{
    struct node *temp;
    temp=malloc(sizeof(struct node));
    temp->data=num;
    if (head==NULL)
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        // please change your logic
    }
    return head;
}

int  main()
{
    struct node *head;
    head=NULL;
    head = add(20,head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}

Cheers! 干杯!

Change 更改

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

To

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

The reason you are getting a segmentation fault, is because in malloc, you allocated sizeof(struct node*) , which is basically enough size for a pointer and doesn't make any sense to do so. 出现分段错误的原因是,在malloc中,您分配了sizeof(struct node*) ,该大小对于指针而言基本上足够,并且这样做没有任何意义。

Also, change the logic of the else in add, if you are planning to add in more nodes. 另外,如果您计划添加更多节点,请更改add中else的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM