C 创建相互指向的新节点

C 创建相互指向的新节点,c,pointers,struct,nodes,C,Pointers,Struct,Nodes,当我创建struct x的新节点时,多次使用struct x create时它是如何工作的。我觉得奇怪的是,你可以多次使用它,因为它每次都用相同的名称new将内存分配给struct x?结构的每个节点不都需要单独的名称吗。还是说每次分配新内存都很重要 主要问题:我将创建第一个节点,然后创建第二个节点。然后,第一个节点应指向第二个节点,依此类推。但是当我创建第一个节点时,第二个节点不存在,所以我不能设置first->next=second 我看过链表的例子,但目前它并没有改善我的思维。代码没有我自

当我创建struct x的新节点时,多次使用struct x create时它是如何工作的。我觉得奇怪的是,你可以多次使用它,因为它每次都用相同的名称new将内存分配给struct x?结构的每个节点不都需要单独的名称吗。还是说每次分配新内存都很重要

主要问题:我将创建第一个节点,然后创建第二个节点。然后,第一个节点应指向第二个节点,依此类推。但是当我创建第一个节点时,第二个节点不存在,所以我不能设置first->next=second

我看过链表的例子,但目前它并没有改善我的思维。代码没有我自己的理解和思考那么重要。请帮助我思考和掌握这个概念

//我试图遵循Degustaf的建议(除了下一个指针,基本上与创建新节点相同),但实现错误。所以我想知道这个代码有什么问题:

struct x{
    ...;
    ...;
    struct x * next;
};

struct x create() {
    struct x new = malloc...
    new->... = .;
    new->... = ..;
    new->next = NULL
};
#包括
#包括
#包括
结构x{
INTA;
int b;
结构x*next;
}
结构x*创建(整数a、整数b){
struct x*new=malloc(sizeof(struct x));
新建->a=a;//namn skitsamma分配相关
新建->b=b;
新建->下一步=空;
归还新的;
};
int main(){
结构x*x1=结构x*create(12,13);
返回0;
}

在创建了指针和指针之后,您可以简单地分配它们的值

i、 e


我想这是你想要的,但这是一个列表中有加强编号的示例,你也可以根据自己的意愿更改代码

struct x x1 = create();
struct x x2 = create();
x1.next = &x2;
x2.next = &x1;
#包括
#包括
#包括
#包括
结构单元{
浮动信息;
结构单元*下一步;
};
整数更多(浮点*k)
{
char-ans[4];
printf(“继续是/否:”);
scanf(“%s”,ans);
if(ans[0]=='Y'){
printf(“插入值:”);
scanf(“%f”,k);
申报表(1);
}
其他的
返回(0);
}
结构单元*crelist()
{
结构单元*last=(结构单元*)NULL;
结构单元*ptr=(结构单元*)空;
结构单元*列表=(结构单元*)空;
浮动k;
ptr=(结构单元*)malloc(sizeof(结构单元));
如果(ptr!=(结构单元*)为空){
printf(“插入值:”);
scanf(“%f”、&k);
ptr->info=k;
ptr->next=(结构单元*)空;
列表=ptr;
last=ptr;
}
其他的
返回((结构单元*)NULL);
while(更多(&k)){
ptr=(结构单元*)malloc(sizeof(结构单元));
如果(ptr!=(结构单元*)为空){
ptr->info=k;
ptr->next=(结构单元*)空;
最后->下一步=ptr;
last=ptr;
}
其他的
打破
}
返回(列表);
}
无效打印列表(结构单元*列表)
{
结构细胞*p;
p=列表;
而(p!=(结构单元*)为空){
printf(“->%f\n”,(*p).info);
p=(*p);
}
返回;
}
int main()
{
结构单元*列表;
int i;
list=crelist();
打印列表(列表);
scanf(“%d”、&i);
系统(“暂停”);
返回0;
}
还是说每次分配新内存都很重要

但是,您的代码还有其他问题。特别是,您没有从
create
函数返回任何内容。我认为有两种方法可以解决这个问题。首先,您可以直接返回结构,这意味着您不需要malloc:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
struct cell {
   float info;
   struct cell * next;
};
int more (float * k)
{
char ans[4];

printf("Continue Yes/No: ");
scanf("%s",ans);
if (ans[0]=='Y') {
   printf("insert value: ");
   scanf("%f",k);
   return(1);
}
else
    return(0);
}

struct cell * crelist()
{
 struct cell * last = (struct cell *)NULL;
 struct cell * ptr  = (struct cell *)NULL;
 struct cell * list = (struct cell *)NULL;
 float k;

 ptr = (struct cell *)malloc(sizeof(struct cell));
 if (ptr != (struct cell *)NULL) {
    printf("insert value: ");
    scanf("%f",&k);
    ptr->info = k;
    ptr->next = (struct cell *)NULL;
    list = ptr;
    last = ptr;
 }
 else
     return((struct cell *)NULL);
 while (more(&k)) {
       ptr = (struct cell *)malloc(sizeof(struct cell));
       if (ptr != (struct cell *)NULL) {
          ptr->info = k;
          ptr->next = (struct cell *)NULL;
          last->next = ptr;
          last = ptr;
       }
       else
           break;
 }
        return(list);
 }

void printlist(struct cell * list)
{
  struct cell * p;

  p = list;
  while (p != (struct cell *)NULL) {
        printf("->%f\n",(*p).info);
        p=(*p).next;
  }
  return;
}

int main()
{
  struct cell * list;
  int i;

  list = crelist();
  printlist(list);
  scanf("%d",&i);
  system("pause");
  return 0;
}
然后您可以使用

struct x create()
{
    struct x new;
    new.member1 = .;
    new.member2 = ..;
    new.next = NULL;
    return new;
};
struct x *create()
{
    struct x *new  = malloc...;
    new->member1 = .;
    new->member2 = ..;
    new->next = NULL;
    return new;
};
另一种可能是返回一个指向结构的指针,在这种情况下,它将变为

struct x x1 = create();
struct x x2 = create();
x1.next = &x2;
然后您可以使用

struct x create()
{
    struct x new;
    new.member1 = .;
    new.member2 = ..;
    new.next = NULL;
    return new;
};
struct x *create()
{
    struct x *new  = malloc...;
    new->member1 = .;
    new->member2 = ..;
    new->next = NULL;
    return new;
};

我的意见是,第二种选择更简洁,因为您不需要担心链表中的单个元素超出范围,尽管在释放内存时确实需要小心(需要遍历列表并一次释放一个元素。

struct x new=malloc…
应该是
struct x*new=malloc…
。您还需要返回
*new
。但是我会诚实地只使用指针(返回指针等)。我认为您实际上不希望链表包含循环。如果我调用struct x*create(),是否应该使用struct x*create()而不是struct x*x1=create()调用它;?你的建议似乎很好,我试图用你现在可以在我的问题的同一帖子中看到的代码创建一个结构。我执行了错误的代码,但不知道是什么错误。将
main
函数的主体更改为
struct x*x1=*create(12,13)