创建具有其他列表元素的列表(结构相同) [英] Creating a list with elements other lists (same struct)

查看:54
本文介绍了创建具有其他列表元素的列表(结构相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个列表:list1,list2,list3.

Lets say i have 3 lists : list1,list2,list3.

这些列表的每个元素的 struct :

Their struct of each element of those lists :

struct node {
    char value[20] ;
    struct node * next ;    
    int occurs;
} ;

typedef struct node Node;
typedef Node * List;

但是我不认为这很重要.

but i dont think it matters.

我想创建一个新列表,但是它的每个元素都必须是这3个列表中的每一个.对此,我的新 struct 是(正确的):

I want to create a new list but every element of it has to be each of those 3 lists.My new struct for this matter is that(its correct):

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

所以我不确定乞求是否正确,但是我如何填写(并正确)以获取列表列表?

So im not sure if the begging is even correct but how will i fill it(and correct) in order to achieve the list of lists?

推荐答案

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;
    }
}

这篇关于创建具有其他列表元素的列表(结构相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆