Advertisement
Eduardo_Pires

Trabaio di C

Aug 17th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.41 KB | Software | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct list List;
  6. typedef struct node Node;
  7.  
  8. struct list
  9. {
  10.     Node *head;
  11. };
  12.  
  13. struct node
  14. {
  15.     int info;
  16.     Node *next;
  17. };
  18.  
  19. Node *newNode()
  20. {
  21.     Node *n;
  22.     n = (Node *)malloc(sizeof(Node));
  23.  
  24.     return n;
  25. }
  26.  
  27. void deleteNode(Node *n)
  28. {
  29.     if (n != NULL)
  30.         free(n);
  31. }
  32.  
  33. List *createList()
  34. {
  35.     List *list;
  36.     list = (List *)malloc(sizeof(List));
  37.  
  38.     if (list != NULL)
  39.     {
  40.         list->head = NULL;
  41.     }
  42.  
  43.     return list;
  44. }
  45.  
  46. bool isFull(List *list)
  47. {
  48.     return false;
  49. }
  50.  
  51. void push(List *list, int X)
  52. {
  53.     Node *pAux;
  54.     Node *last;
  55.  
  56.     if (!isFull(list))
  57.     {
  58.         pAux = newNode();
  59.         pAux->info = X;
  60.         pAux->next = NULL;
  61.  
  62.         last = list->head;
  63.  
  64.         if (list->head == NULL)
  65.         {
  66.             list->head = pAux;
  67.             return;
  68.         }
  69.  
  70.         while (last->next != NULL)
  71.         {
  72.             last = last->next;
  73.         }
  74.  
  75.         last->next = pAux;
  76.     }
  77. }
  78.  
  79. bool isEmpty(List *list)
  80. {
  81.     return (list->head == NULL);
  82. }
  83.  
  84. void pop(List *list, int *X)
  85. {
  86.     Node *pAux;
  87.  
  88.     if (!isEmpty(list))
  89.     {
  90.         pAux = list->head;
  91.         *X = pAux->info;
  92.         list->head = pAux->next;
  93.         deleteNode(pAux);
  94.     }
  95. }
  96.  
  97. void showAllListItems(List *list)
  98. {
  99.     int c;
  100.  
  101.     while (!isEmpty(list))
  102.     {
  103.         pop(list, &c);
  104.         printf("%c ", c);
  105.     }
  106.  
  107.     puts("");
  108. }
  109.  
  110. void destroyList(List *list)
  111. {
  112.     int c;
  113.  
  114.     while (!isEmpty(list))
  115.     {
  116.         pop(list, &c);
  117.     }
  118.  
  119.     free(list);
  120. }
  121.  
  122.  
  123. // Funções principais
  124.  
  125. // pertence
  126. bool PertenceRec(int element, Node *n)
  127. {
  128.     if (element == n->info)
  129.     {
  130.         return 1;
  131.     }
  132.     else
  133.     {
  134.         if (n->next == NULL)
  135.         {
  136.             return 0;
  137.         }
  138.         else
  139.         {
  140.             PertenceRec(element, n->next);
  141.         }
  142.     }
  143. }
  144.  
  145. bool pertence(int element, List *list)
  146. {
  147.     Node *n;
  148.     n = list->head;
  149.  
  150.     return PertenceRec(element, n);
  151. }
  152.  
  153. // ultimo
  154. int ultimoRec(Node *n)
  155. {
  156.     if (n->next == NULL)
  157.     {
  158.         return n->info;
  159.     }
  160.     else
  161.     {
  162.         ultimoRec(n->next);
  163.     }
  164. }
  165.  
  166. int ultimo(List *list)
  167. {
  168.     Node *n;
  169.     n = list->head;
  170.  
  171.     return ultimoRec(n);
  172. }
  173.  
  174. // soma
  175. int somaRec(Node *n, int X)
  176. {
  177.     if (n == NULL)
  178.     {
  179.         return X;
  180.     }
  181.     else
  182.     {
  183.         somaRec(n->next, X += n->info);
  184.     }
  185. }
  186.  
  187. int soma(List *list)
  188. {
  189.     Node *n;
  190.     n = list->head;
  191.     int X = 0;
  192.  
  193.     return somaRec(n, X);
  194. }
  195.  
  196. //soma ímpares
  197. int somaImparesRec(Node *n, int X)
  198. {
  199.     if (n == NULL)
  200.     {
  201.         return X;
  202.     }
  203.     else
  204.     {
  205.         if(n->info%2 != 0)
  206.         {
  207.             somaImparesRec(n->next, X += n->info);
  208.         }
  209.         else
  210.         {
  211.             somaImparesRec(n->next, X);
  212.         }
  213.     }
  214. }
  215.  
  216. int somaImpares(List *list)
  217. {
  218.     Node *n;
  219.     n = list->head;
  220.     int X = 0;
  221.  
  222.     return somaImparesRec(n, X);
  223. }
  224.  
  225. //n-ésimo
  226. int nEzimoRec(Node *N, int comparator, int position)
  227. {
  228.     // caso base, interrompe a funcao.
  229.     if (position == comparator)
  230.     {
  231.         return N->info;
  232.     }
  233.  
  234.     nEzimoRec(N->next, comparator+1, position);
  235. }
  236.  
  237. int nEzimo(int position, List *list)
  238. {
  239.     Node *n;
  240.     n = list->head;
  241.     int comparator = 1;
  242.  
  243.  
  244.     return nEzimoRec(n, comparator, position);
  245. }
  246.  
  247. // comprimento
  248.  
  249. int comprimentoRec(Node *N, int X)
  250. {
  251.     // caso base, interrompe a funcao.
  252.     if (N == NULL)
  253.         return X;
  254.  
  255.     comprimentoRec(N->next, X+1);
  256. }
  257.  
  258. int comprimento(List *L)
  259. {
  260.     Node *p;
  261.     int N;
  262.     N = 0;
  263.  
  264.     p = L->head;
  265.     return comprimentoRec(p, N);
  266. }
  267.  
  268. // Função Main
  269.  
  270. int main()
  271. {
  272.     List *list;
  273.     list = createList();
  274.  
  275.     int inputForBelong, inputForNEsim, elementsOfTheList, input, sum = 0, i = 0;
  276.     scanf("%d %d", &inputForBelong, &inputForNEsim);
  277.  
  278.  
  279.     while (scanf("%d", &elementsOfTheList) != EOF)
  280.     {
  281.         push(list, elementsOfTheList);
  282.     }
  283.  
  284.     /*
  285.         while (i <= 9)
  286.         {
  287.             scanf("%d", &input);
  288.             push(list, input);
  289.             i++;
  290.  
  291.         }
  292.     */
  293.  
  294.  
  295.     printf("%d\n", pertence(inputForBelong, list));
  296.     printf("%d\n", ultimo(list));
  297.     printf("%d\n", soma(list));
  298.     printf("%d\n", somaImpares(list));
  299.     printf("%d\n", nEzimo(inputForNEsim, list));
  300.     printf("%d\n", comprimento(list));
  301.  
  302.     return 0;
  303. }
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement