Advertisement
Eduardo_Pires

trabalho Variaveis formatadas

Aug 17th, 2022 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 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 newValue)
  52. {
  53.     Node *pAux;
  54.     Node *last;
  55.  
  56.     if (!isFull(list))
  57.     {
  58.         pAux = newNode();
  59.         pAux->info = newValue;
  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.  
  80. bool isEmpty(List *list)
  81. {
  82.     return (list->head == NULL);
  83. }
  84.  
  85.  
  86. void pop(List *list, int *withdrawnValue)
  87. {
  88.     Node *pAux;
  89.  
  90.     if (!isEmpty(list))
  91.     {
  92.         pAux = list->head;
  93.         *withdrawnValue = pAux->info;
  94.         list->head = pAux->next;
  95.         deleteNode(pAux);
  96.     }
  97. }
  98.  
  99.  
  100. void showAllListItems(List *list)
  101. {
  102.     int item;
  103.  
  104.     while (!isEmpty(list))
  105.     {
  106.         pop(list, &item);
  107.         printf("%d ", item);
  108.     }
  109.  
  110.     printf("/n");
  111. }
  112.  
  113. void destroyList(List *list)
  114. {
  115.     int item;
  116.  
  117.     while (!isEmpty(list))
  118.     {
  119.         pop(list, &item);
  120.     }
  121.  
  122.     free(list);
  123. }
  124.  
  125.  
  126. // Funções principais
  127.  
  128. // pertence
  129. bool PertenceRec(int element, Node *no)
  130. {
  131.     if (element == no->info)
  132.     {
  133.         return 1;
  134.     }
  135.     else
  136.     {
  137.         if (no->next == NULL)
  138.         {
  139.             return 0;
  140.         }
  141.         else
  142.         {
  143.             PertenceRec(element, no->next);
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149. bool pertence(int element, List *list)
  150. {
  151.     Node *no;
  152.     no = list->head;
  153.  
  154.     return PertenceRec(element, no);
  155. }
  156.  
  157. // ultimo
  158. int ultimoRec(Node *no)
  159. {
  160.     if (no->next == NULL)
  161.     {
  162.         return no->info;
  163.     }
  164.     else
  165.     {
  166.         ultimoRec(no->next);
  167.     }
  168. }
  169.  
  170.  
  171. int ultimo(List *list)
  172. {
  173.     Node *no;
  174.     no = list->head;
  175.  
  176.     return ultimoRec(no);
  177. }
  178.  
  179. // soma
  180. int somaRec(Node *no, int valorSoma)
  181. {
  182.     if (no == NULL)
  183.     {
  184.         return valorSoma;
  185.     }
  186.     else
  187.     {
  188.         somaRec(no->next, valorSoma += no->info);
  189.     }
  190. }
  191.  
  192. int soma(List *list)
  193. {
  194.     Node *no;
  195.     no = list->head;
  196.     int valorSoma = 0;
  197.  
  198.     return somaRec(no, valorSoma);
  199. }
  200.  
  201.  
  202. //soma ímpares
  203. int somaImparesRec(Node *no, int valorSomaImpares)
  204. {
  205.     if (no == NULL)
  206.     {
  207.         return valorSomaImpares;
  208.     }
  209.     else
  210.     {
  211.         if(no->info%2 != 0)
  212.         {
  213.             somaImparesRec(no->next, valorSomaImpares += no->info);
  214.         }
  215.         else
  216.         {
  217.             somaImparesRec(no->next, valorSomaImpares);
  218.         }
  219.     }
  220. }
  221.  
  222. int somaImpares(List *list)
  223. {
  224.     Node *no;
  225.     no = list->head;
  226.     int valorSomaImpares = 0;
  227.  
  228.     return somaImparesRec(no, valorSomaImpares);
  229. }
  230.  
  231. //n-ésimo
  232. int nEzimoRec(Node *no, int comparador, int position)
  233. {
  234.     // caso base, interrompe a funcao.
  235.     if (position == comparador)
  236.     {
  237.         return no->info;
  238.     }
  239.  
  240.     nEzimoRec(no->next, comparador+1, position);
  241. }
  242.  
  243. int nEzimo(int position, List *list)
  244. {
  245.     Node *no;
  246.     no = list->head;
  247.     int comparador = 1;
  248.  
  249.  
  250.     return nEzimoRec(no, comparador, position);
  251. }
  252.  
  253.  
  254. // comprimento
  255.  
  256. int comprimentoRec(Node *no, int tamanho)
  257. {
  258.     // caso base, interrompe a funcao.
  259.     if (no == NULL)
  260.         return tamanho;
  261.  
  262.     comprimentoRec(no->next, tamanho+1);
  263. }
  264.  
  265.  
  266. int comprimento(List *L)
  267. {
  268.     Node *no;
  269.     int tamanho;
  270.     tamanho = 0;
  271.  
  272.     no = L->head;
  273.     return comprimentoRec(no, tamanho);
  274. }
  275.  
  276. // Função Main
  277.  
  278. int main()
  279. {
  280.     List *list;
  281.     list = createList();
  282.    
  283.     //int i;
  284.     int inputPertence, inputNEzimo, elementosDaLista;
  285.     scanf("%d %d", &inputPertence, &inputNEzimo);
  286.  
  287.  
  288.     while (scanf("%d", &elementosDaLista) != EOF)
  289.     {
  290.         push(list, elementosDaLista);
  291.     }
  292.  
  293.     /*
  294.     while (i <= 9)
  295.     {
  296.         scanf("%d", &elementosDaLista);
  297.         push(list, elementosDaLista);
  298.         i++;
  299.  
  300.     }
  301.     */
  302.  
  303.  
  304.     printf("%d\n", pertence(inputPertence, list));
  305.     printf("%d\n", ultimo(list));
  306.     printf("%d\n", soma(list));
  307.     printf("%d\n", somaImpares(list));
  308.     printf("%d\n", nEzimo(inputNEzimo, list));
  309.     printf("%d\n", comprimento(list));
  310.  
  311.     return 0;
  312. }
  313.  
  314.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement