calgorithmdata-structuresstructdisjoint-sets

Disjoint set data structure in c


I want to implement the structure on the image in C language. But I have a problem because the 2 structures created call each other and therefore pose a problem how can I remedy it?

set data structure picture : Imgur link

liste set :

typedef struct liste_set
{
    set *head;
    set *tail;
} liste_set;

set :

typedef struct set set;
struct set
{
    int value;
    liste_set *liste;
    set *next;
};

Thank you for your help.


Solution

  • All you have to do is have preceeding declerations so the compiler knows what set is even before you actually fully declare it.

    // declare structs
    struct liste_set;
    struct set;
    
    // typedef struct x to just x
    typedef struct liste_set liste_set;
    typedef struct set set;
    
    // describe what the structs are
    struct liste_set
    {
        set *head;
        set *tail;
    };
    
    struct set
    {
        int value;
        liste_set *liste;
        set *next;
    };