Guest User

Untitled

a guest
Jan 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define declare_list_type(TypeElement, TypeNode)                \
  6.     typedef struct Node_##TypeNode *TypeNode;        \
  7.     struct Node_##TypeNode {                                    \
  8.         TypeElement *element;                                   \
  9.         TypeNode next_ptr;                           \
  10.         int size;                                               \
  11.     };                                                         \
  12.  
  13. #define init_list(TypeNode, lista)                              \
  14.     lista  = (TypeNode) malloc(sizeof(TypeNode));               \
  15.     lista->next_ptr = NULL;                                     \
  16.     lista->size = 0;                                               \
  17.  
  18. #define add_to_list(TypeElement, TypeNode, lista, elemento)     \
  19. do {                                                           \
  20.     TypeNode before_ptr;                                       \
  21.     TypeNode after_ptr;                                        \
  22.     TypeNode new_item_ptr;                                     \
  23.     before_ptr = lista;                                         \
  24.     after_ptr =  before_ptr->next_ptr;                          \
  25.                                                                 \
  26.     if(lista->size == 0)                                       \
  27.     {                                                           \
  28.         lista->element = malloc(sizeof(TypeElement));       \
  29.         *(lista->element) = elemento;                     \
  30.         lista->size = lista->size+1;                                    \
  31.         lista->next_ptr = NULL;                             \
  32.                                                                 \
  33.     }                                                           \
  34.     else                                                        \
  35.     {                                                           \
  36.        while (after_ptr != NULL)                                   \
  37.         {                                                           \
  38.           after_ptr = after_ptr->next_ptr;                          \
  39.           before_ptr = before_ptr->next_ptr;                        \
  40.         }                                                           \
  41.                                                                     \
  42.         new_item_ptr = (TypeNode)malloc(sizeof(TypeNode));            \
  43.         new_item_ptr->element = malloc(sizeof(TypeElement));        \
  44.          *(new_item_ptr->element) = elemento;                        \
  45.                                                                     \
  46.         lista->size = lista->size+1;                               \
  47.         before_ptr->next_ptr = new_item_ptr;                        \
  48.         new_item_ptr->next_ptr = after_ptr;                         \
  49.     }                                                               \
  50.  } while (0)                                                        \
  51.  
  52. #define  print(TypeElement, TypeNode, lista)                            \
  53. do {                                                                    \
  54.     TypeNode cur_ptr;                                                   \
  55.     for (cur_ptr = lista; cur_ptr != NULL; cur_ptr = cur_ptr->next_ptr) \
  56.         printf("%d ", (*(cur_ptr->element)).peso);                      \
  57.     printf("\n");                                                       \
  58. }while(0)                                                               \
  59.  
  60.  
  61.  
  62.  
  63. int main()
  64. {
  65.  
  66.     typedef struct
  67.     {
  68.         int peso;
  69.     } cebolla;
  70.  
  71.     typedef struct
  72.     {
  73.         int peso;
  74.     } rabanito;
  75.  
  76.     declare_list_type(rabanito, lista_de_rabanitos);
  77.  
  78.  
  79.     declare_list_type(cebolla, lista_de_cebollas);
  80.  
  81.     lista_de_cebollas cebollas_de_verdeo;
  82.     lista_de_rabanitos rabanitos_azules;
  83.  
  84.  
  85.  
  86.     const rabanito un_raba  = {1};
  87.     const rabanito un_raba2 = {2};
  88.     const rabanito un_raba3 = {3};
  89.     const rabanito un_raba4 = {4};
  90.  
  91.     const cebolla cebolla_1  = {11};
  92.     const cebolla cebolla_2 = {22};
  93.     const cebolla cebolla_3 = {33};
  94.  
  95.  
  96.     init_list(lista_de_rabanitos, rabanitos_azules);
  97.     init_list(lista_de_cebollas, cebollas_de_verdeo);
  98.  
  99.     add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba);
  100.     add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba2);
  101.     add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba3);
  102.     add_to_list(rabanito, lista_de_rabanitos, rabanitos_azules, un_raba4);
  103.  
  104.     add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo,cebolla_1);
  105.     add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo, cebolla_2);
  106.     add_to_list(cebolla, lista_de_cebollas, cebollas_de_verdeo, cebolla_3);
  107.  
  108.     printf("Lista de rabanitos azules \n");
  109.     print(rabanito, lista_de_rabanitos, rabanitos_azules );
  110.     printf("Lista de cebollas de verdeo\n");
  111.     print(cebolla, lista_de_cebollas, cebollas_de_verdeo );
  112.  
  113.  
  114.     return 0;
  115. }
Add Comment
Please, Sign In to add comment