首页 > 解决方案 > 如何在C中创建一个包含另一个列表的列表

问题描述

所以我想创建一个主列表abbones的子列表liste_fav,这是结构

  /*********************************/
typedef struct liste_fav
{
       signed char numf[20];
       struct liste_fav *suiv;

}liste_fav;

/***********************************/

typedef struct abonnes// lz structure principale
{
    signed char  num_c[20];
    signed  char operateur [20];
    signed char profil[20];
    int credit;
    liste_fav *liste;// on sait pas si on doit mettre une etoile ici
    struct abonnes *adr;
}abonnes;

si 我创建了一个列表,其中包含我想创建的主列表,其中包含有关订户的信息和他的信息,我们有一个指向另一个子列表的指针,其中包含该订户的收藏号码。我想创建一个应用程序,允许订阅者操作​​他最喜欢的号码列表(添加一个新号码,或删除......),在此之前我必须在主列表和子列表中记录数据。我做了这个功能,可以让我们填写信息,也可以填写喜欢的号码列表,请你帮忙 PS:我有一个疑问是:字段中子列表头部的做作( liste_fav *liste)

使用的功能:

liste_fav *allouer_fav ()// allouer un espace memoire, elle retourne l'adresse de l'espace alloue
{
    return ((liste_fav*)malloc(sizeof(liste_fav)));
}


abonnes *allouer_abo () 
{
    return ((abonnes*)malloc(sizeof(abonnes)));
}

void creation_abo(abonnes **R)
    {
        signed char ope [20],numm[20],prof[20],rr[20],num[20];
        abonnes *s,*tet;
        liste_fav *b,*a;
        //(*R)->liste=NULL;
        int i,cred,no,eee;
        int k=1;
        printf(" number of nodes to create : ");
        scanf("%d",&no);//the number of the nodes to link of the list abonnes
        printf("~~~~~~~~~~~~\n");
        *R= allouer_abo();
        printf("operateur  %d :",k);
        scanf("%s",&ope);
        aff_operateur(R,ope);//strcpy((p -> operateur ),r);
        printf("his number:");
        scanf("%s",&numm);
        aff_num_c(R,numm);//strcpy((p -> num_c),num)
        printf("donnez son credit : ");
        scanf("%d",&cred);
        aff_credit(R,cred);// p-> credit =n;
        printf("donnez son profil : ");
        scanf("%s",&prof);
        aff_profil(R,prof);//strcpy((p->profil),f)
        printf("creation de la liste des favoris: ");
        printf("donnez le nombre de mailons a creer :");
        scanf("%d",&eee);
        printf("donnez le numero du contact 1 : ");
        scanf("%s",&rr);
       // abonnes *pp = (*R )->liste -> numf;

         //strcpy(pp,rr);
         abonnes *J= (*R)->liste ;//I have doubt here, it's supposed to be the affectation oh the head of the list in J
         J=allouer_fav();//return ((liste_fav*)malloc(sizeof(liste_fav)));
         affnum_fav_propre(J,rr);//strcpy((p ->numf ),n);

         for ( i=1;i<eee;i++)
         {

        b=allouer_fav();
        printf("donnez le numero du contact %d :",i+1);
        scanf("%s",&num);
        affnum_fav_propre(b,num);strcpy((p ->numf ),n);
        aff_adr_fav_propre(J,b);// s -> suiv = q; s a pointer to list abonnes


    }
    aff_adr_fav_propre(b,NULL);


// we affected the heads, now we will affect the other structures to link
        tet= R;
        for(i=1;i<no;i++)
        {
            k++;
            s= allouer_abo();
            printf("_-__-_-_-_-_-_-_-_-_-_-_-_-_\n");


                printf("give the operateur %d :",k);
             scanf("%s",&ope);
            aff_operateur(s,ope);
            printf("donnez son numero : ");
            scanf("%s",&numm);
            aff_num_c(s,numm);
            printf("the credit : ");
            scanf("%d",&cred);
             aff_credit(s,cred);
            printf("the  profil : ");
           scanf("%s",&prof);
           aff_profil(s,prof);
            printf("creation  of list favoris: ");
        printf(" numbre of nodes you wanna create :");
        scanf("%d",&eee);
        printf(" numero the favoris 1 : ");
        scanf("%s",&rr);
        abonnes *J= (*R)->liste ;
         J=allouer_fav();
         affnum_fav_propre(J,rr);


         for ( i=1;i<eee;i++)
         {

        b=allouer_fav();
        printf("numero  %d :",i+1);
        scanf("%s",&num);
        affnum_fav_propre(b,num);// strcpy((p ->numf ),n);
        aff_adr_fav_propre(J,b);// s -> suiv = q;
        J=b;


    }
   // aff_adr_fav_propre(b,NULL);
             aff_adr_abo(R,s);//p-> adr=q
             R=s;

        }
        aff_adr_abo(R,NULL);
        aff_adr_fav_propre(b,NULL);

        //s=NULL;
        ////tet=NULL; i was not sure
        return ( tet);

    }

有什么帮助或建议吗?希望你明白我的问题

标签: c

解决方案


是的,你的怀疑是正确的。这一行:

abonnes *J= (*R)->liste;

不会给你想要的结果。此外,J 需要是 a liste_fav*,而不是 a abonnes*。像这样:

liste_fav* J = allouer_fav();
(*R)->liste = J;

您需要在下面的代码中进行相同的更改。

[顺便说一句,如果您创建一个填充单个abonnes实例的函数,并为头部调用一次,然后在“影响其他要链接的结构”循环中调用它,那么您的生活会更轻松。实际上,您有大量重复的代码,您必须修复两次。]

我有疑问,aff_adr_fav_propre因为您没有显示代码,并且注释是错误的[它不是“指向列表 abonnes 的指针”,而是“指向 liste_fav 的指针”]。它需要执行以下操作:

J->suiv = b;
J = b;

您在文章中也没有提到返回结果时遇到的问题。您尝试返回tet,但您已声明creation_abo无效。我认为您想要做的是将其声明如下:

abonnes *creation_abo()
{
<existing code>
    return tet;
}

调用者看起来像这样:

abonnes *master_list = creation_abo();

一致的缩进和描述性变量名称也会改善您的生活。


推荐阅读