Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct list List;
- typedef struct node Node;
- struct list
- {
- Node *head;
- };
- struct node
- {
- int info;
- Node *next;
- };
- Node *newNode()
- {
- Node *n;
- n = (Node *)malloc(sizeof(Node));
- return n;
- }
- void deleteNode(Node *n)
- {
- if (n != NULL)
- free(n);
- }
- List *createList()
- {
- List *list;
- list = (List *)malloc(sizeof(List));
- if (list != NULL)
- {
- list->head = NULL;
- }
- return list;
- }
- bool isFull(List *list)
- {
- return false;
- }
- void push(List *list, int X)
- {
- Node *pAux;
- Node *last;
- if (!isFull(list))
- {
- pAux = newNode();
- pAux->info = X;
- pAux->next = NULL;
- last = list->head;
- if (list->head == NULL)
- {
- list->head = pAux;
- return;
- }
- while (last->next != NULL)
- {
- last = last->next;
- }
- last->next = pAux;
- }
- }
- bool isEmpty(List *list)
- {
- return (list->head == NULL);
- }
- void pop(List *list, int *X)
- {
- Node *pAux;
- if (!isEmpty(list))
- {
- pAux = list->head;
- *X = pAux->info;
- list->head = pAux->next;
- deleteNode(pAux);
- }
- }
- void showAllListItems(List *list)
- {
- int c;
- while (!isEmpty(list))
- {
- pop(list, &c);
- printf("%c ", c);
- }
- puts("");
- }
- void destroyList(List *list)
- {
- int c;
- while (!isEmpty(list))
- {
- pop(list, &c);
- }
- free(list);
- }
- // Funções principais
- // pertence
- bool PertenceRec(int element, Node *n)
- {
- if (element == n->info)
- {
- return 1;
- }
- else
- {
- if (n->next == NULL)
- {
- return 0;
- }
- else
- {
- PertenceRec(element, n->next);
- }
- }
- }
- bool pertence(int element, List *list)
- {
- Node *n;
- n = list->head;
- return PertenceRec(element, n);
- }
- // ultimo
- int ultimoRec(Node *n)
- {
- if (n->next == NULL)
- {
- return n->info;
- }
- else
- {
- ultimoRec(n->next);
- }
- }
- int ultimo(List *list)
- {
- Node *n;
- n = list->head;
- return ultimoRec(n);
- }
- // soma
- int somaRec(Node *n, int X)
- {
- if (n == NULL)
- {
- return X;
- }
- else
- {
- somaRec(n->next, X += n->info);
- }
- }
- int soma(List *list)
- {
- Node *n;
- n = list->head;
- int X = 0;
- return somaRec(n, X);
- }
- //soma ímpares
- int somaImparesRec(Node *n, int X)
- {
- if (n == NULL)
- {
- return X;
- }
- else
- {
- if(n->info%2 != 0)
- {
- somaImparesRec(n->next, X += n->info);
- }
- else
- {
- somaImparesRec(n->next, X);
- }
- }
- }
- int somaImpares(List *list)
- {
- Node *n;
- n = list->head;
- int X = 0;
- return somaImparesRec(n, X);
- }
- //n-ésimo
- int nEzimoRec(Node *N, int comparator, int position)
- {
- // caso base, interrompe a funcao.
- if (position == comparator)
- {
- return N->info;
- }
- nEzimoRec(N->next, comparator+1, position);
- }
- int nEzimo(int position, List *list)
- {
- Node *n;
- n = list->head;
- int comparator = 1;
- return nEzimoRec(n, comparator, position);
- }
- // comprimento
- int comprimentoRec(Node *N, int X)
- {
- // caso base, interrompe a funcao.
- if (N == NULL)
- return X;
- comprimentoRec(N->next, X+1);
- }
- int comprimento(List *L)
- {
- Node *p;
- int N;
- N = 0;
- p = L->head;
- return comprimentoRec(p, N);
- }
- // Função Main
- int main()
- {
- List *list;
- list = createList();
- int inputForBelong, inputForNEsim, elementsOfTheList, input, sum = 0, i = 0;
- scanf("%d %d", &inputForBelong, &inputForNEsim);
- while (scanf("%d", &elementsOfTheList) != EOF)
- {
- push(list, elementsOfTheList);
- }
- /*
- while (i <= 9)
- {
- scanf("%d", &input);
- push(list, input);
- i++;
- }
- */
- printf("%d\n", pertence(inputForBelong, list));
- printf("%d\n", ultimo(list));
- printf("%d\n", soma(list));
- printf("%d\n", somaImpares(list));
- printf("%d\n", nEzimo(inputForNEsim, list));
- printf("%d\n", comprimento(list));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement