[英]Linked list Implementation in C with structure
I'm using this structure as a linked list: 我正在使用此结构作为链接列表:
typedef struct Node{
int value;
struct node_t* next;
}node_t;
And everything works fine until I put struct node_t* next
before int value
field, then I have a lot of trash values working with that structure. 一切正常,直到我将
struct node_t* next
放在int value
字段之前,然后我有很多垃圾值处理该结构。 Is it about wrong implementation or something else in the code? 它是关于错误的实现还是代码中的其他内容?
You are calling your structure Node
and defining a node_t
type. 您正在调用结构
Node
并定义node_t
类型。 Then you are using node_t
as if it was the name of the structure and not the type. 然后你使用
node_t
就好像它是结构的名称而不是类型。
Try this 试试这个
typedef struct node {
int value;
struct node *next;
} Node;
Or 要么
typedef struct node Node;
struct node {
int value;
Node *node;
};
If you call it struct Node
, then 如果你称之为
struct Node
,那么
struct Node {
int value;
/* The compiler doesn't know what `struct Node' is yet */
struct Node *next;
/* But you can always declare pointers, even of types the compiler
* doesn't know everything about. Because the size of a pointer
* does not depend on the type of the pointee.
*/
};
In your example, it's even worse. 在你的例子中,情况更糟。 You
typedef
ed something that is a new type as the compiler understand it, to use it you MUST not use struct
. 您
typedef
编的东西,是一种新型的编译器理解它,使用它,你不能使用struct
。 The whole idea behind typedef
ing is that you DEFINED a new type, so suppose the following typedef
背后的整个想法是你定义了一个新类型,所以假设如下
typedef struct Node node;
then to declare a pointer of type node
( note, again node
IS A TYPE ), 然后声明一个
node
类型的指针( 注意,再次node
是一个类型 ),
node *anode;
but you attempted something like 但你尝试了类似的东西
struct node *anode;
and it's wrong because there is no struct node
in the code above, it's struct Node
. 这是错误的,因为上面的代码中没有
struct node
,它是struct Node
。
Another mistake in your code is that, the node_t
type does not exist when the compiler finds the 代码中的另一个错误是,当编译器找到时,
node_t
类型不存在
struct node_t *next;
which is already wrong because if the type were defined before the struct which is possible like this 这已经是错误的,因为如果类型是在结构之前定义的,这是可能的
typedef struct Node node_t
it'd still be wrong to use struct
on the node_t
type, because for the compiler node_t
is not a struct
it's a new type, which in turn is simply an alias for struct Node
. 在
node_t
类型上使用struct
仍然是错误的,因为对于编译器而言, node_t
不是struct
它是一种新类型,而这又是struct Node
的别名。
Typedefing structures in my experience is more trouble than benefit anyway. 根据我的经验,Typedefing结构比无论如何更有麻烦。 And it's not so hard to type
struct Something
instead of just Something
. 并且输入
struct Something
而不仅仅是Something
并不是那么难。 It also has the benefit of being more explicit, so if another programmer reads your code, they will immediately know that Something
is a struct
. 它还具有更明确的优点,因此如果另一个程序员读取您的代码,他们将立即知道
Something
是一个struct
。
Note : I deliberately changed the name to node
because it's considered bad practice to suffix your own defined types with _t
. 注意 :我故意将名称更改为
node
因为用_t
自己定义的类型后缀被认为是不好的做法。 It's not necessarily a bad thing, but over the years that I've been working with this I developed some habits and one of them is not to use _t
as a suffix for my own defined types. 这不一定是件坏事,但多年来我一直在研究这个问题,我养成了一些习惯,其中一个就是不使用
_t
作为我自己定义的类型的后缀。 Which by the way only exist in my code if they will improve readability a lot. 顺便说一句,只有这样才能提高可读性。 Otherwise I simply use the name of the structure with the
struct
keyword. 否则,我只需使用
struct
关键字的结构名称。
You are using a non existing type node_t. 您正在使用非现有类型node_t。 The type doesn't exist because the type
struct Node
is not even complete and you are using it's alias. 该类型不存在,因为类型
struct Node
甚至不完整,并且您正在使用它的别名。 Another thing to remember while using typedefs
with structures don't use struct keyword along with alias eg. 使用带结构的
typedefs
时要记住的另一件事是不使用struct关键字和别名,例如。
/* This is correct */
typedef struct Node
{
int x;
struct Node *next;
} node_t;
/* while these are incorrect */
/* Prefixing struct keyword to a typedef'ed type */
struct node_t *listhead;
/* The type is inclomplete and you are using an alias of the type
which doesn't even exist */
typedef struct Node
{
int x;
node_t *next;
};
You are trying to create a pointer to the structure which you are yet to create. 您正在尝试创建指向您尚未创建的结构的指针。 So, it should have been,
那应该是,
typedef struct Node{
int value;
struct Node* next;
}node_t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.