gpt4 book ai didi

c - c中链表的子列表?

转载 作者:行者123 更新时间:2023-11-30 15:48:42 25 4
gpt4 key购买 nike

我想使用 c 在单个列表中创建一个子列表。我有一份名单,上面有名字、姓氏、电话、电子邮件……。我想在电话下创建一个子列表以保留更多电话。这是我的结构:

typedef struct ph{
char * phone;
ph *next;
}listofphones;

typedef struct client{
char* name;
char* surname;
date birthday;
char bankaccount[16];
listofphone phone;
char* mail;
struct client *next;
} clientData;

我想要为任何客户提供一个额外的子列表。问题是这些手机都在同一个列表中。那么我怎样才能创建一个不同的列表呢?

示例:

name1->surname1->birthday1->bankaccount1->phone1->mail1.......
|
phone2
|
phone3
.
.
.

(抱歉,画得不好,我希望它足够清晰。)

最佳答案

您只需在此列表的每个节点中保留另一个列表的头。您的结构定义将类似于:

typedef struct ph{
char * phone;
ph *next;
}listofphones;

typedef struct client{
char* name;
char* surname;
date birthday;
char bankaccount[16];
ph* phHead;
char* mail;
struct client *next;
} clientData;

现在,您已经有了一个列表中的列表。然而,编写代码来遍历、查找、枚举客户端的电话号码将需要双指针间接寻址,最好避免这种情况。如果您的客户电话号码数量有限,您可以将其更改为:

typedef struct client{
char* name;
char* surname;
date birthday;
char bankaccount[16];
char phNumbers[5][10];
char* mail;
struct client *next;
} clientData;

上述结构体定义假设每个客户端最多有 5 个电话号码,每个电话号码最多有 10 个字符。您可以根据需要更改此设置。

关于c - c中链表的子列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com