假设我有3个列表:list1,list2,list3。
这些列表中每个元素的struct
:
struct node {
char value[20] ;
struct node * next ;
int occurs;
} ;
typedef struct node Node;
typedef Node * List;
但我不认为这很重要。
我想创建一个新列表,但它的每个元素都必须是这3个列表中的每一个。我的新问题struct
就是那个(它的正确):
typedef struct listoflists{
List list;
struct listoflists*next;
}Nested;
我的新功能列表:
void createlistoflists(Nested **LIST,List list1,List list2,List list3){
if (*LIST==NULL){
*LIST=listab;
}
else
所以我不确定乞讨是否正确,但我将如何填写(并更正)以获得列表清单?
答案 0 :(得分:1)
Nested* Nested_create(List list) {
Nested* new = malloc(sizeof(Nested));
new->list = list;
new->next = NULL;
return new;
}
void Nested_add(Nested** proot, Nested* node) {
if (*proot == NULL) {
*proot = node;
} else {
Nested* cur = *proot;
while (cur->next)
cur = cur->next;
cur->next = node;
}
}
void createlistoflists(Nested **LIST, List list1, List list2, List list3) {
Nested_add(LIST, Nested_create(list1));
Nested_add(LIST, Nested_create(list2));
Nested_add(LIST, Nested_create(list3));
}
在单一功能中:
void createlistoflists(Nested **LIST, List list1, List list2, List list3) {
List lists[] = {list1, list2, list3};
for (List* it = lists; it < lists + 3; ++it) {
Nested* node = malloc(sizeof(Nested));
node->list = *it;
node->next = NULL;
*LIST = node;
LIST = &(*LIST)->next;
}
}