PedroHMM

Pilhas

May 8th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<malloc.h>
  5.  
  6.  
  7. typedef struct estrutura
  8. {
  9.     int valor;
  10.     estrutura *prox;   
  11. }NO;
  12.  
  13. typedef struct
  14. {
  15.     NO* topo;
  16. }PILHA;
  17.  
  18.  
  19. void inicializar(PILHA *p)
  20. {
  21.     p->tapo =-1;
  22. }
  23.  
  24. void push(PILHA *p, int valor)
  25. {
  26.     NO* novo = (NO*) malloc(sizeof(NO));
  27.     novo->valor = valor;
  28.     novo->prox = p->topo;
  29.     p->topo = novo;
  30. }
  31.  
  32. int pop(PILHA *p)
  33. {
  34.     if(p->topo < 0) return -1;
  35.     int resposta =  p->topo->valor;
  36.     NO* aux = p->topo;
  37.     p->topo = p->topo->prox;
  38.     free(aux);
  39.     return resposta;
  40. }
  41.  
  42. void imprimir(PILHA *p)
  43. {
  44.     NO* c = p->topo;
  45.     while(c)
  46.     {
  47.         printf("%1 ", c->valor);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment