Link C 中的另一个结构
Link another structure in C
我正尝试在 C 中完成此数据结构。将另一个结构连接到另一个结构。像这样:
struct room
{
int roomnumber;
struct room * nextRoom;
struct person * personList;
}*top=NULL,*temp=NULL,top1;
struct person
{
int personnumber;
struct person *next;
}*node=NULL,temp1;
struct room 有一个指向 struct person 的指针。我很难将它连接到另一个结构。这是正确的吗?
这是我的函数
void insert()
{
int val;
printf("enter value: ");
scanf("%d",&val);
newnode=create_node(val);
if(top->personList==NULL)
{
top->personList=newnode;
}
else
{
node->next=newnode;
node=newnode;
}
}
插入一个人到房间。房间就像已经创建一样。 create_node()
是执行 malloc()
的人
这是一个不使用全局变量的版本,它使您的函数更加通用和可重用:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct person
{
int personNumber;
struct person *next;
} person; /* NOTE: this is a typedef, not a global declaration like you did */
typedef struct room
{
int roomNumber;
person *personList;
person *lastInList;
struct room *nextRoom;
} room; /* NOTE: this is a typedef, not a global declaration like you did */
person *create_person(int val)
{
person *ret = calloc(1, sizeof(*ret));
if (ret)
{
ret->personNumber = val;
ret->next = NULL;
}
return ret;
}
int room_insert(room *r, int val)
{
person *p = create_person(val);
if (NULL == p)
return -1;
if (NULL != r->personList)
r->lastInList->next = p;
else
r->personList = p;
r->lastInList = p;
return 0;
}
int main(int argc, char **argv)
{
room my_room = { 0 }; /* important! initializes pointers to NULL */
room my_other_room = { 0 }; /* important! initializes pointers to NULL */
person *p;
room_insert(&my_room, 5);
room_insert(&my_other_room, -5);
room_insert(&my_room, 10);
room_insert(&my_other_room, -10);
room_insert(&my_room, 15);
room_insert(&my_other_room, -15);
for (p = my_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
for (p = my_other_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
return 0;
}
我正尝试在 C 中完成此数据结构。将另一个结构连接到另一个结构。像这样:
struct room
{
int roomnumber;
struct room * nextRoom;
struct person * personList;
}*top=NULL,*temp=NULL,top1;
struct person
{
int personnumber;
struct person *next;
}*node=NULL,temp1;
struct room 有一个指向 struct person 的指针。我很难将它连接到另一个结构。这是正确的吗?
这是我的函数
void insert()
{
int val;
printf("enter value: ");
scanf("%d",&val);
newnode=create_node(val);
if(top->personList==NULL)
{
top->personList=newnode;
}
else
{
node->next=newnode;
node=newnode;
}
}
插入一个人到房间。房间就像已经创建一样。 create_node()
是执行 malloc()
这是一个不使用全局变量的版本,它使您的函数更加通用和可重用:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct person
{
int personNumber;
struct person *next;
} person; /* NOTE: this is a typedef, not a global declaration like you did */
typedef struct room
{
int roomNumber;
person *personList;
person *lastInList;
struct room *nextRoom;
} room; /* NOTE: this is a typedef, not a global declaration like you did */
person *create_person(int val)
{
person *ret = calloc(1, sizeof(*ret));
if (ret)
{
ret->personNumber = val;
ret->next = NULL;
}
return ret;
}
int room_insert(room *r, int val)
{
person *p = create_person(val);
if (NULL == p)
return -1;
if (NULL != r->personList)
r->lastInList->next = p;
else
r->personList = p;
r->lastInList = p;
return 0;
}
int main(int argc, char **argv)
{
room my_room = { 0 }; /* important! initializes pointers to NULL */
room my_other_room = { 0 }; /* important! initializes pointers to NULL */
person *p;
room_insert(&my_room, 5);
room_insert(&my_other_room, -5);
room_insert(&my_room, 10);
room_insert(&my_other_room, -10);
room_insert(&my_room, 15);
room_insert(&my_other_room, -15);
for (p = my_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
for (p = my_other_room.personList; p; p = p->next)
printf("%d\n", p->personNumber);
return 0;
}