Created
May 5, 2011 22:13
-
-
Save carlosbrando/958087 to your computer and use it in GitHub Desktop.
Exemplo simples de pilha.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define SIZE 50 | |
| void push(int i); | |
| int pop(void); | |
| int *tos, *p1, stack[SIZE]; | |
| main(void) | |
| { | |
| int value; | |
| tos = stack; /* faz o tos conter o topo da pilha */ | |
| p1 = stack; /* inicializa p1 */ | |
| do { | |
| printf("Digite o valor: "); | |
| scanf("%d", &value); | |
| if (value != 0) push(value); | |
| else printf("valor do topo é %d\n", pop()); | |
| } while(value != -1); | |
| } | |
| void push(int i) | |
| { | |
| p1++; | |
| if (p1 == (tos+SIZE)) { | |
| printf("Estouro da pilha"); | |
| exit(1); | |
| } | |
| *p1 = i; | |
| } | |
| int pop(void) | |
| { | |
| if (p1 == tos) { | |
| printf("Estouro da pilha"); | |
| exit(1); | |
| } | |
| p1--; | |
| return *(p1+1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment