Skip to content

Instantly share code, notes, and snippets.

@carlosbrando
Created May 5, 2011 22:13
Show Gist options
  • Select an option

  • Save carlosbrando/958087 to your computer and use it in GitHub Desktop.

Select an option

Save carlosbrando/958087 to your computer and use it in GitHub Desktop.
Exemplo simples de pilha.
#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