Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define declare_list_type(TypeElement, TypeNode) \
- typedef struct Node_##TypeNode *TypeNode; \
- struct Node_##TypeNode { \
- TypeElement *element; \
- TypeNode next_ptr; \
- int size; \
- }; \
- #define init_list(TypeNode, lista) \
- lista = (TypeNode) malloc(sizeof(TypeNode)); \
- lista->next_ptr = NULL; \
- lista->size = 0; \
- #define add_to_list(TypeElement, TypeNode, lista, elemento) \
- do { \
- TypeNode before_ptr; \
- TypeNode after_ptr; \
- TypeNode new_item_ptr; \
- before_ptr = lista; \
- after_ptr = before_ptr->next_ptr; \
- \
- if(lista->size == 0) \
- { \
- lista->element = malloc(sizeof(TypeElement)); \
- *(lista->element) = elemento; \
- lista->size = lista->size+1; \
- lista->next_ptr = NULL; \
- \
- } \
- else \
- { \
- while (after_ptr != NULL) \
- { \
- after_ptr = after_ptr->next_ptr; \
- before_ptr = before_ptr->next_ptr; \
- } \
- \
- new_item_ptr = (TypeNode)malloc(sizeof(TypeNode)); \
- new_item_ptr->element = malloc(sizeof(TypeElement)); \
- *(new_item_ptr->element) = elemento; \
- \
- lista->size = lista->size+1; \
- before_ptr->next_ptr = new_item_ptr; \
- new_item_ptr->next_ptr = after_ptr; \
- } \
- } while (0) \
- #define print(TypeElement, TypeNode, lista) \
- do { \
- TypeNode cur_ptr; \
- for (cur_ptr = lista; cur_ptr != NULL; cur_ptr = cur_ptr->next_ptr) \
- printf("%d ", (*(cur_ptr->element)).peso); \
- printf("\n"); \
- }while(0) \
- int main()
- {
- typedef struct
- {
- int peso;
- } cebolla;
- typedef struct
- {
- int peso;
- } rabanito;
- declare_list_type(rabanito, lista_de_rabanitos);
- declare_list_type(cebolla, lista_de_cebollas);
- lista_de_cebollas cebollas_de_verdeo;
- lista_de_rabanitos rabanitos_azules;
- const rabanito un_raba = {1};
- const rabanito un_raba2 = {2};
- const rabanito un_raba3 = {3};
- const rabanito un_raba4 = {4};
- const cebolla cebolla_1 = {11};
- const cebolla cebolla_2 = {22};
- const cebolla cebolla_3 = {33};
- init_list(lista_de_rabanitos, rabanitos_azules);
- init_list(lista_de_cebollas, cebollas_de_verdeo);
- add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba);
- add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba2);
- add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba3);
- add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba4);
- add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo,cebolla_1);
- add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo, cebolla_2);
- add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo, cebolla_3);
- printf("Lista de rabanitos azules \n");
- print(rabanito, lista_de_rabanitos, rabanitos_azules );
- printf("Lista de cebollas de verdeo\n");
- print(cebolla, lista_de_cebollas, cebollas_de_verdeo );
- return 0;
- }
Add Comment
Please, Sign In to add comment