递归调用一个函数来在C中创建一个列表

时间:2018-04-22 14:49:36

标签: c linked-list

我想创建一个获取列表的函数(即使NULL)并在头顶添加一个元素。 我尝试执行以下代码,但是出现了编译错误:

expected expression before ‘ListaDiElementi’
   crea(&ListaDiElementi lista);

我的代码如下:

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

struct elemento
{
    int info;
    struct element* next;
};

typedef struct element ElementOfList;
typedef ElementOfList * ListOfElements;

typedef ListOfElements crea (ListOfElements list)
{
    ListOfElements new = malloc (sizeof(ElementOfList));
    scanf("%d", &new->info);
    new->next=list;
    return new;
}
int main()
{
    ListOfElements list = NULL;
    do
    {
        crea(&ListOfElements list);
        printf("%d", list->info);
    }
    while(list->info>0);
}

我知道这就像#34;请执行我的代码&#34;但是我真的被困住了,我对如何做到这一点并不知道。

1 个答案:

答案 0 :(得分:1)

你不应该在函数调用中传递类型,而且list已经是指针,所以不要使用&amp; :所以不要使用

crea(&ListOfElements list);

使用

crea(list);

typedef 也不能用于返回类型,因此函数签名应为:

ListOfElements crea (ListOfElements list){.....}