0

I want to make a sub list in a single list using c. I have a list with name, surname, phone, e-mail…. I want to make a sub list under phone to keep more phones. This is my struct:

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;    

I want an extra sub-list for any client. The problem is that the phones are all in the same list. So how can I create a different list?

Example:

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

(Sorry for bad drawing I hope it’s clear enough.)

4

2 回答 2

1

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

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;  

上面的 struct 定义假设每个客户端最多有 5 个电话号码,每个电话号码最多有 10 个字符。您可以根据自己的需要进行更改。

于 2013-05-21T19:45:16.270 回答
0

为什么要制作另一个列表?管理指针将成为一个令人头疼的问题。你不能简单地做

typedef struct client{
char* name;
char* surname;
date birthday;
char bankaccount[16];
int **phone;
char* mail;
struct client *next;
} clientData;
于 2013-05-21T19:34:08.327 回答