Linked list, joining structures

Question:

I'm having some difficulty in joining two structures in the same linked list, I've never worked with generic lists or even associated two structures in an activity, I have the following structures:

struct Patio {
    char iden_patio;
    int capacidade;
    struct Patio *prox;
};  

typedef struct Patio patio;

struct Rec_Emp {    
    char nome_rec[10];
    char ident_rec[10];
    int uso_rec;
    int taxa_rec;
    struct Rec_Emp *prox;
};    

typedef struct Rec_Emp recuperadora;

I wanted to associate these two lists without necessarily uniting all the variables, more or less like this, for each patio, associating the elements of the reclaimers, eg: iden_patio: A, capacity: 10000, name_rec: Rec01, use_rec: 0, etc… How I said I could even unite the two, form a larger struct, but I would like a more elegant solution, as I wanted to preserve the identities of the patios and fireplaces, I appreciate your attention.

Answer:

With the information I have about your problem, I can already give you two answers.

Bearing in mind that you need to know which stoves are associated with a patio, I recommend doing this:

#define RECS_PATIO 2

struct Rec_Emp {
    char nome_rec[10];
    char ident_rec[10];
    int uso_rec;
    int taxa_rec;
    struct Rec_Emp *prox;
};

typedef struct Rec_Emp recuperadora;

struct Patio {
    char iden_patio;
    int capacidade;
    struct Patio *prox;
    struct Rec_Emp *lista[RECS_PATIO];
}; 

typedef struct Patio patio;

Using the structures above, let's suppose you want to associate a reclaimer with a yard and then print some value from the reclaimer by accessing the yard. Do it like this:

p.lista[0] = &r;
int resp = 0;
resp = p.lista[0]->uso_rec; // Acessando uso_rec da recuperadora pela variável do pátio

Bearing in mind that you need to know which patio is associated with a stove, I recommend doing this:

struct Patio {
    char iden_patio;
    int capacidade;
    struct Patio *prox;
}; 

typedef struct Patio patio;

struct Rec_Emp {
    char nome_rec[10];
    char ident_rec[10];
    int uso_rec;
    int taxa_rec;
    struct Rec_Emp *prox;
    struct Patio *p;
};

typedef struct Rec_Emp recuperadora;

Using the code above, let's suppose you want to associate a yard to a reclaimer and then print some yard value by accessing the reclaimer. Do it like this:

r.p = &p;
int resp = 0;
resp = r.p->capacidade;

I hope you have resolved your doubts, anything I am available.

Reference: My knowledge of AED's.

Scroll to Top