链表在C中用结构实现

时间:2016-07-22 08:38:39

标签: c struct linked-list structure

我将此结构用作链接列表:

 typedef struct Node{
       int value;
       struct node_t* next;

 }node_t;

一切正常,直到我将struct node_t* next放在int value字段之前,然后我有很多垃圾值使用该结构。 它是关于错误的实现还是代码中的其他内容?

3 个答案:

答案 0 :(得分:4)

您正在调用结构Node并定义node_t类型。然后你正在使用node_t,就好像它是结构的名称而不是类型。

试试这个

typedef struct node {
    int value;
    struct node *next;
} Node;

或者

typedef struct node Node;
struct node {
    int value;
    Node *node;
};

如果您将其称为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.
     */
};

在你的例子中,情况更糟。你typedef编译了一些新类型,因为编译器理解它,使用它你不能使用structtypedef背后的整个想法是你定义了一个新类型,所以假设以下

typedef struct Node node;

然后声明类型node的指针(注意,再次node是一个类型),

node *anode;

但您尝试了类似

的内容
struct node *anode;

并且它错了,因为上面的代码中没有struct node,它是struct Node

代码中的另一个错误是,当编译器找到

时,node_t类型不存在
struct node_t *next;

这已经错了,因为如果类型是在结构之前定义的那么可能是这样的

typedef struct Node node_t

struct类型上使用node_t仍然是错误的,因为编译器node_t不是struct它是新的type,它只是struct Node的别名。

根据我的经验,Typedefing结构比起任何好处都要麻烦。并且输入struct Something而不仅仅是Something并不是那么难。它还具有更明确的好处,因此如果其他程序员读取您的代码,他们会立即知道Somethingstruct

注意:我故意将名称更改为node,因为使用_t为您自己定义的类型添加后缀被认为是不好的做法。它不一定是坏事,但多年来我一直在努力解决这个问题,我养成了一些习惯,其中一个习惯是不使用_t作为我自己定义类型的后缀。顺便说一句,只有这样才能提高可读性。否则,我只需使用struct关键字的结构名称。

答案 1 :(得分:1)

您使用的是非现有类型node_t。该类型不存在,因为类型struct Node甚至不完整,您正在使用它的别名。将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;
};

答案 2 :(得分:1)

您正在尝试创建指向您尚未创建的结构的指针。所以,应该是,

typedef struct Node{
int value;
struct Node* next;
}node_t;