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 newValue)
- {
- Node *pAux;
- Node *last;
- if (!isFull(list))
- {
- pAux = newNode();
- pAux->info = newValue;
- 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 *withdrawnValue)
- {
- Node *pAux;
- if (!isEmpty(list))
- {
- pAux = list->head;
- *withdrawnValue = pAux->info;
- list->head = pAux->next;
- deleteNode(pAux);
- }
- }
- void showAllListItems(List *list)
- {
- int item;
- while (!isEmpty(list))
- {
- pop(list, &item);
- printf("%d ", item);
- }
- printf("/n");
- }
- void destroyList(List *list)
- {
- int item;
- while (!isEmpty(list))
- {
- pop(list, &item);
- }
- free(list);
- }
- // Funções principais
- // pertence
- bool PertenceRec(int element, Node *no)
- {
- if (element == no->info)
- {
- return 1;
- }
- else
- {
- if (no->next == NULL)
- {
- return 0;
- }
- else
- {
- PertenceRec(element, no->next);
- }
- }
- }
- bool pertence(int element, List *list)
- {
- Node *no;
- no = list->head;
- return PertenceRec(element, no);
- }
- // ultimo
- int ultimoRec(Node *no)
- {
- if (no->next == NULL)
- {
- return no->info;
- }
- else
- {
- ultimoRec(no->next);
- }
- }
- int ultimo(List *list)
- {
- Node *no;
- no = list->head;
- return ultimoRec(no);
- }
- // soma
- int somaRec(Node *no, int valorSoma)
- {
- if (no == NULL)
- {
- return valorSoma;
- }
- else
- {
- somaRec(no->next, valorSoma += no->info);
- }
- }
- int soma(List *list)
- {
- Node *no;
- no = list->head;
- int valorSoma = 0;
- return somaRec(no, valorSoma);
- }
- //soma ímpares
- int somaImparesRec(Node *no, int valorSomaImpares)
- {
- if (no == NULL)
- {
- return valorSomaImpares;
- }
- else
- {
- if(no->info%2 != 0)
- {
- somaImparesRec(no->next, valorSomaImpares += no->info);
- }
- else
- {
- somaImparesRec(no->next, valorSomaImpares);
- }
- }
- }
- int somaImpares(List *list)
- {
- Node *no;
- no = list->head;
- int valorSomaImpares = 0;
- return somaImparesRec(no, valorSomaImpares);
- }
- //n-ésimo
- int nEzimoRec(Node *no, int comparador, int position)
- {
- // caso base, interrompe a funcao.
- if (position == comparador)
- {
- return no->info;
- }
- nEzimoRec(no->next, comparador+1, position);
- }
- int nEzimo(int position, List *list)
- {
- Node *no;
- no = list->head;
- int comparador = 1;
- return nEzimoRec(no, comparador, position);
- }
- // comprimento
- int comprimentoRec(Node *no, int tamanho)
- {
- // caso base, interrompe a funcao.
- if (no == NULL)
- return tamanho;
- comprimentoRec(no->next, tamanho+1);
- }
- int comprimento(List *L)
- {
- Node *no;
- int tamanho;
- tamanho = 0;
- no = L->head;
- return comprimentoRec(no, tamanho);
- }
- // Função Main
- int main()
- {
- List *list;
- list = createList();
- //int i;
- int inputPertence, inputNEzimo, elementosDaLista;
- scanf("%d %d", &inputPertence, &inputNEzimo);
- while (scanf("%d", &elementosDaLista) != EOF)
- {
- push(list, elementosDaLista);
- }
- /*
- while (i <= 9)
- {
- scanf("%d", &elementosDaLista);
- push(list, elementosDaLista);
- i++;
- }
- */
- printf("%d\n", pertence(inputPertence, list));
- printf("%d\n", ultimo(list));
- printf("%d\n", soma(list));
- printf("%d\n", somaImpares(list));
- printf("%d\n", nEzimo(inputNEzimo, list));
- printf("%d\n", comprimento(list));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement