Data Structures - List Differences

Asked

Viewed 142 times

2

Please would like to know the real difference between these structures below:

typedef struct {
 int info;
 struct lista * prox
 struct lista * ant;
} tipo_lista;

and that:

struct noCliente {
 int tempoUtilizandoMesa;
 struct noCliente *ant;
 struct noCliente *prox;
};

typedef struct noCliente *CLIENTE;
  • I think in the first example it is incomplete; it would not be typedef struct lista { ... } tipo_lista;?

  • 1

    That one lista here you have no.

  • So this struct lista* of ant and prox refers to another structure...

1 answer

2


In addition to the members' names the first creates a new type that can be used anywhere in the code where a type fits, so tipo_lista becomes as much of a guy as int is a guy.

The second creates a structure called noCliente. This doesn’t create a type, so if you want to instantiate it in an object you have to make it type struct noCliente, since the guy is struct with a specialization.

Of course soon after this structure is used to create a type called CLIENTE which will always be a pointer, so it will be a type by reference that is not the pattern of structures. The use of the pointer required separation between the structure declaration and the type declaration.

  • So to sum up, can I use both? Is the result the same? Because notice a difference in the statement of functions also: following the first 1 have: tipo_lista * inserir_inicio (tipo_lista * p, tipo_lista * novo_no){ already following the second example we have: void adicionarFilaNormal(CLIENTE filaNormal, int tempoUtilizando){

  • 2

    You can, but there’s always a more suitable one in every situation. I prefer whenever possible and it makes sense to already create the type and only create separately when there is no other way. It did not have the functions in the question, but it is precisely what I said, to get the type since the reference (the built-in pointer) needs to be of the second form, there it is abstracted. If you want to use by reference in the first case you have to do manual. What can be a good way to make it clearer depends on the semantics you want and the style adopted.

  • Because I have a structure code based on the second option with reference, but my teacher teaches the same as the first example, so for his project I will do based on the first option, with pointers in the parameters of the functions.

  • 1

    @André in general C programmers like to be explicit more, it’s a lot in this language. I hope he does so and not because everyone does so. It is always important to know why he is doing something, even when it gives a space.

Browser other questions tagged

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