C中的新节点,如何修复结构?

C中的新节点,如何修复结构?,c,struct,nodes,C,Struct,Nodes,我无法理解这个概念的全部含义。让我困惑的主要问题是结构中的指针在结构中。。。基本上我理解的是我想要创建一个节点链 当我运行这个程序时,它会在两秒钟后崩溃。我相信我在main.c中的结构有问题,因为我是自己创建的,正如你所看到的,我真的是薄冰上的斑比 main.c #include <stdlib.h> #include <assert.h> #include <stdio.h> #include "list.h" // I guess my struct

我无法理解这个概念的全部含义。让我困惑的主要问题是结构中的指针在结构中。。。基本上我理解的是我想要创建一个节点链

当我运行这个程序时,它会在两秒钟后崩溃。我相信我在main.c中的结构有问题,因为我是自己创建的,正如你所看到的,我真的是薄冰上的斑比

main.c

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

#include "list.h"

// I guess my struct is not correct
static struct {
    LIST node;
    int item;
} node;


int main() {

    list_create();

    list_append(node.node, node.item);
}
struct node* list_create() {

 struct node* head = (struct node*) malloc(sizeof (struct node));
 head->next = NULL;
 return head;

}

void list_append(struct node* n, int item)
{

    /* Create new node */
    struct node* new_node = (struct node*) malloc (sizeof (struct node));
    new_node->item = item;


    /* Find last link */
    while (n->next) {
        n = n->next;
    }

    /* Joint the new node */
    new_node->next = NULL;
    n->next = new_node;
}
list.c

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

#include "list.h"

// I guess my struct is not correct
static struct {
    LIST node;
    int item;
} node;


int main() {

    list_create();

    list_append(node.node, node.item);
}
struct node* list_create() {

 struct node* head = (struct node*) malloc(sizeof (struct node));
 head->next = NULL;
 return head;

}

void list_append(struct node* n, int item)
{

    /* Create new node */
    struct node* new_node = (struct node*) malloc (sizeof (struct node));
    new_node->item = item;


    /* Find last link */
    while (n->next) {
        n = n->next;
    }

    /* Joint the new node */
    new_node->next = NULL;
    n->next = new_node;
}

您正在调用list\u create,但不使用其结果。

首先,节点用于创建包含数据的结构,并且在该数据中有指向另一个结构的指针

static struct {
    LIST node;
    int item;
} node;
实际上你的结构是不正确的

必须在开始时使用数据创建结构,例如:

static struct node{
    int item;
};
然后将指针指向类似的结构,但不会有相同的数据=>

static struct node{
    struct node *next;
    int item;
};
您可以使用此指针来操作其他结构

我在你的主要问题中看到了另一个问题:

您可以调用函数“list_create()”,该函数返回指向结构的指针,但不指定任何内容

必须创建指向struct的指针,然后按如下方式分配它:

int main() {

struct node *list;
    list = list_create();
}

此代码可以完全工作(您可以将其全部放在一个C文件中;代码中有注释):

#包括
#包括
#包括
/*我删除了“static”,因为我们想要的是一个类型,
不是全局静态变量*/
/*这个“节点”是我们的列表元素。它有一个指向下一个元素的指针,并且
有些数据,在本例中是int*/
类型定义结构节点{
结构节点*下一步;
国际项目;
}节点;
/*为方便起见,另一种类型是指向节点的指针*/
typedef节点*列表;
/*创建一个列表就像创建一个节点一样简单,然后做“下一步”
指针为空,这是正确的*/
结构节点*列表_创建(){
结构节点*头=(结构节点*)malloc(sizeof(结构节点));
head->next=NULL;
回流头;
}
/*这个附加代码中没有错误*/
void list_append(结构节点*n,int项)
{
/*创建新节点*/
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建节点->项目=项目;
/*查找最后一个链接*/
while(n->next){
n=n->next;
}
/*连接新节点*/
新建节点->下一步=空;
n->next=新节点;
}
/*我添加了以下内容以确保其正常工作:)*/
无效打印列表(列表l){
列表tmp=l;
while(tmp){
printf(“%d\n”,tmp->item);
tmp=tmp->next;
}
/*下面是一些更改。我创建了一个本地列表(基本上是一个指针)
到一个节点,记得吗?)并使用list_create初始化它。然后,我调用
列表\u追加两次以将一些额外数据放入其中。
很好用*/
int main(){
LIST myList=LIST_create();
列表附加(myList,10);
附加列表(myList,13);
打印列表(myList);
}

好的,我不完全理解这一点,但为什么不给结构指定一个名称?我的意思是结构的名称不应该在这些字符之间
,类似于此
}节点
以便我可以键入
节点。item
?有不同的方法来命名一个结构,首先是:struct name{};但是你可以使用“typedef”,我见过有人制作这样的结构
struct node{}head={0,NULL}。在我看来,他们首先创建了一个名为
节点
的结构。然后他们将其重命名为
head
?!:(调用结构有typedef,比如
typedef结构s_节点{}node;
。然后,当你创建一个结构时,你可以做
struct s_node
node
。噢,事情变得更清楚了,但我不明白另一个结构中的结构指针是如何工作的。我猜该指针有一些关于下一个节点的信息,不…?我也不知道
typedef str是什么uct节点*列表;
可以。我不明白它…“它只是工作。”。这可能是因为指针总是相同的,编译器并不需要指针的确切类型才能声明它。毕竟,它只是一个内存地址。
typedef struct node*列表是三部分:
typedef
告诉编译器您正在定义一个新类型。
struct node*
是您正在为其定义新的类型,最后,
LIST
是typedef的名称。尽管这很方便,但您可以将
LIST
替换为
struct node*
,代码运行完全相同。这解释了一点额外的原因: