Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- typedef struct no
- {
- void *info;
- struct no *prox;
- }NO;
- typedef struct pilha
- {
- NO *inicio;
- int Nelementos;
- }PILHA;
- PILHA *CriarPilha()
- {
- PILHA *P = (PILHA *)malloc(sizeof(PILHA));
- P->inicio = NULL;
- P->Nelementos = 0;
- return P;
- }
- void PUSH(PILHA *Plenha, void *item)
- {
- NO *aux = (NO *)malloc(sizeof(NO));
- aux->info = item;
- aux->prox = Plenha->inicio;
- Plenha->inicio = aux;
- Plenha->Nelementos++;
- }
- void *POP(PILHA *P)
- {
- if (!P->inicio)
- return NULL;
- void *Valor = P->inicio->info;
- NO *aux = P->inicio->prox;
- free(P->inicio);
- P->inicio = aux;
- P->Nelementos--;
- return Valor;
- }
- bool vazia(PILHA *p)
- {
- return !p || !p->inicio;
- }
- float factorial(int n)
- {
- PILHA *p = CriarPilha();
- for (; n > 0; n--)
- {
- int *y = (int *)malloc(sizeof(int));
- *y = n;
- PUSH(p, y);
- }
- float res = 1;
- while (!vazia(p))
- res *= *((int*)POP(p));
- return res;
- }
- void main()
- {
- printf("%f\n", factorial(5));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement