简体   繁体   English

制作链接列表

[英]Making a Linked List

typedef struct{
    char name[25];
    int yearOfBirth;
    int district;
    char gender;
    int age;

    CitizenType *next;

}CitizenType;

When I try to make a Linked List in this format in Visual Studio, I get all sorts of errors relating to syntax. 当我尝试在Visual Studio中以这种格式制作“链接列表”时,出现各种与语法有关的错误。 (102 in total) but when I comment out the CitizenType *next; (总共102个),但是当我注释掉CitizenType * next;时; I get no errors. 我没有错误。 I realize it has something to do with referencing the structure before it has been completely declared, but I have no idea how to fix this. 我意识到它与在结构完全声明之前引用结构有关,但是我不知道如何解决此问题。

Declare the typedef before (and separately from) the structure. 在结构之前(与结构分开)声明typedef。

typedef struct citizen_type CitizenType;

struct citizen_type {
    ...
    CitizenType *next;
};

The problem is that 问题是

CitizenType

enters into the namespace of types only after the structure ends. 仅在结构结束后才进入类型的名称空间。

So you can use the new type only after its declaration. 因此,只有在声明新类型后才能使用它。

You can use instead of it a struct name (giving a name to the struct), or declaring the type before to declare the structure with a name, as in previous post. 您可以代替使用struct name (为struct name ),也可以在使用名称声明结构之前声明类型,如上一篇文章所述。

Try this: 尝试这个:

typedef struct node{
    char name[25];
    int yearOfBirth;
    int district;
    char gender;
    int age;

    struct node *next;

}CitizenType;

Check this stack overflow answer for more information about self referenced structs in C. From the answer: 检查堆栈溢出答案以获取有关C中自引用结构的更多信息。

A CitizenType cannot contain another CitizenType as it becomes a never-ending recursion. 一个CitizenType不能包含另一个CitizenType,因为它成为无休止的递归。

Hope it helps! 希望能帮助到你!

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

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