Platinum2d

List - Modular code

Jun 6th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. //basic.h
  2.  
  3. #ifndef BASIC_H
  4. #define BASIC_H
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13.  
  14. typedef int element;
  15.  
  16. typedef struct list_element {
  17.     element value;
  18.     struct list_element *next;
  19. }item;
  20.  
  21. typedef item* list;
  22.  
  23. extern list emptylist();
  24. extern void showList(list l);
  25. extern bool empty(list l);
  26. extern element head(list l);
  27. extern void* _abort();
  28. extern list tail(list l);
  29. extern list cons(element e, list l);
  30. extern list copyList(list l);
  31. extern void destroy(list l);
  32. extern list insert_FIFO(element e, list l);
  33. extern list toList(element *v, uint32_t size);
  34. extern element* toArray(list l, size_t *sz);
  35. extern list copyList(list l);
  36. extern list insert();
  37.  
  38. #endif /*!BASIC_H*/
  39.  
  40. //basic.c
  41.  
  42. list emptylist()
  43. {
  44.     return NULL;
  45. }
  46.  
  47. bool empty(list l)
  48. {
  49.     return (l == NULL);
  50. }
  51.  
  52. element head(list l)
  53. {
  54.     if (empty(l)) { _abort(); return EXIT_FAILURE; }
  55.  
  56.     return l->value;
  57. }
  58.  
  59. void* _abort()
  60. {
  61.     printf("\n\nIMPOSSIBILE ACCEDERE ALLA LISTA\n\n");
  62.     return NULL;
  63. }
  64.  
  65. list tail(list l)
  66. {
  67.     if (empty(l))
  68.     {
  69.         _abort();
  70.         return emptylist();
  71.     }
  72.     else
  73.         return l->next;
  74. }
  75.  
  76. list cons(element e, list l)
  77. {
  78.     list t;
  79.  
  80.     t = malloc(sizeof(item));
  81.     t->value = e;
  82.     t->next = l;
  83.  
  84.     return t;
  85. }
  86.  
  87. void showList(list l)
  88. {
  89.     list temp = l;
  90.  
  91.     printf("[");
  92.     while (temp != NULL)
  93.     {
  94.         printf("%d", temp->value);
  95.         if (!empty(tail(temp))) printf(", ");
  96.         temp = tail(temp);
  97.     }
  98.     printf("]");
  99. }
  100.  
  101. list insert()
  102. {
  103.     list l;
  104.     uint32_t len;
  105.  
  106.     printf("Inserire il numero di elementi che si desidera inserire (il numero deve essere > di zero) : ");
  107.  
  108.     do
  109.     {
  110.         scanf("%d", &len);
  111.     } while (len <= 0); printf("\n");
  112.  
  113.     l = NULL;
  114.  
  115.     for (uint32_t i = 0; i < len; i++)
  116.     {
  117.         int val;
  118.         printf("\nInserire l'elemento n.%d : ", i + 1);
  119.         scanf("%d", &val);
  120.  
  121.         l = cons(val, l);
  122.     }
  123.  
  124.     return l;
  125. }
  126.  
  127. void destroy(list l)
  128. {
  129.     while (tail(l) != NULL)
  130.     {
  131.         list prossimo = tail(l);
  132.         free(l);
  133.         l = prossimo;
  134.     }
  135. }
  136.  
  137. list copyList(list l)
  138. {
  139.     list temp = l, t = emptylist();
  140.  
  141.     while (!empty(temp))
  142.     {
  143.         t = insert_FIFO(head(temp), t);
  144.         temp = tail(temp);
  145.     }
  146.  
  147.     return t;
  148. }
  149.  
  150. list insert_FIFO(element e, list l)
  151. {
  152.     list l1 = l, t, start = l;
  153.  
  154.     t = cons(e, l);
  155.     if (empty(l)) return t;
  156.  
  157.     while (!empty(l))
  158.     {
  159.         l1 = l;
  160.         l = tail(l);
  161.     }
  162.  
  163.     l1->next = t;
  164.     t->next = emptylist();
  165.  
  166.     return start;
  167. }
  168.  
  169. list toList(element *v, uint32_t size)
  170. {
  171.     list l = emptylist();
  172.  
  173.     for (size_t i = 0; i < size; i++)
  174.         l = cons(v[i], l);
  175.  
  176.     return l;
  177. }
  178.  
  179. element *toArray(list l, size_t *sz)
  180. {
  181.     element *v = NULL;
  182.     size_t size = 0;
  183.     list t = l;
  184.  
  185.     while (!empty(t))
  186.     {
  187.         v = realloc(v, (++size) * sizeof(element));
  188.         v[size - 1] = head(t);
  189.         t = tail(t);
  190.     }
  191.  
  192.     *sz = size;
  193.     return v;
  194. }
  195.  
  196. int main(void)
  197. {
  198.     return EXIT_SUCCESS;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment