Chained list, union of structures

Asked

Viewed 469 times

3

I have some difficulty in uniting two structures in the same chained list, I have never worked with generic lists or even associated in one activity two structures, 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, associate the elements of the recuperaras, ex: iden_patio: A, capacity: 10000, nome_rec: Rec01, uso_rec: 0, etc... As I said could even unite the two, form a larger struct, but I would like a more elegant solution, because I wanted to preserve the identities of the patios and recuperaras, I thank you attention from now.

  • How exactly did you want to associate these structures? What is the purpose of the association?

  • Not to lose the identity of the structures, in the EP that I am doing, to each patio is associated one or more recuperaras (the Ep is about the logistics of a port), thus preserving the structures, the two separate, would be a more elegant and clearer solution for those who contact the code (the teacher, p. ex.), and even for me. I made the union of the structures, I found very messy

  • Well, I can give you a full answer. However I need some more information: Is there a limit to recoveries for a patio? You intend to make this association to know which recuperators are associated with a patio or, which patio is associated with such recuperara?

  • For each patio associo until two recuperaras, then the patio B would have the recuperaras Rec01 and Rec02, the patio A, would only have the recuperara Rec01, but the case of A, would be the only one with only one recuperara, all the other patios will have two recuperaras.

  • me ajuda https://answall.com/questions/412245/lista-encadeada-socorro

1 answer

2


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

Taking into account that you need to know which recuperators are associated with a patio, I recommend doing so:

#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 above structures, let’s assume that you want to associate a recuperator to a patio p and then print some value of the recuperator accessing through the patio. 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

Taking into account that you need to know which patio is associated with a recuperator, I recommend doing so:

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 assume that you want to associate a patio p with a recuperator and then print some yard value accessing by the recuperator. Do it like this:

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

I hope you’ve cleared up your doubts, anything I’m up for.

Reference: My knowledge in AED’s.

  • This structure that passed me I believe I understood, but now another question arose that I had not come across, I even managed to insert the values of the structure individually, in this case I climbed from a file . txt, however I could not associate to each patio the information of the recuperaras. (I refer specifically to the first example, with " struct Rec_emp *list[RECS_PATIO];"

  • The constant RECS_PATIO means recuperators per patio. Each patio will have a list of recuperators of size RECS_PATIO. Understood this, if you want to associate a recuperator to a patio, just include the recuperator in the list of the desired patio. Doing this you will have associated to the courtyard, all the information of the recuperator. If that’s not your question, tell me so I can open a chat room. We can talk about it at will.

  • Right. In the insert function I am going up the data of a file . txt, I have in the file have, e. g. , " 600000 B REC01 REC03 0 0 6000 8000 "which are respectively capacity, identification of the courtyard (these two in the courtyard structure), recuperator 1 and 2, use 1 and 2, recovery rate 1 and 2 (these associated with the recuperative structure), I am banging head in the following, qdo insiro no file, Should I make one for each structure first? or I can insert straight, type: new.list[0]->taxa_rec = tx_recup1, new.list[1]->taxa_rec = tx_recup2.

  • I can’t call you on chat right now because I’m at work, but if I’m still having second thoughts, can I call you tonight? Note: in the recovery structure, it excludes the variable char ident_rec[10];, because as this new format it loses meaning.

  • Sure, I’m waiting for the chat so we can discuss it better!

  • http://chat.stackexchange.com/rooms/info/56331/associacao-de-lista-encadeada?tab=general

  • I entered the chat and posted my questions, but you did not appear rsrs

  • Carlos, you can chat?

  • Sorry for the delay, I answered what you asked me in the chat

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.