Pilha


SUBMITTED BY: mathheusgois

DATE: April 18, 2017, 7 p.m.

FORMAT: Text only

SIZE: 870 Bytes

HITS: 19798

  1. #define MaxPilha 10
  2. struct TpPilhaM2
  3. {
  4. int Base [MaxPilha], Topo[MaxPilha];
  5. int Pilhas[MaxPilha];
  6. };
  7. void Inicializa (TpPilhaM2 &PM, int QtdePilhas)
  8. {
  9. int qtde, i;
  10. qtde = MaxPilha / QtdePilhas;
  11. for (i = 0; i <= QtdePilhas; i++)
  12. {
  13. PM.Base[i] = i * qtde;
  14. PM.Topo[i] = i * qtde - 1;
  15. }
  16. }
  17. int Vazia (TpPilhaM2 PM, int NrP)
  18. {
  19. return PM.Base[NrP - 1] > PM.Topo[NrP - 1];
  20. }
  21. int Cheia (TpPilhaM2 PM, int NrP)
  22. {
  23. return PM.Topo[NrP - 1] + 1 == PM.Base[NrP];
  24. }
  25. void Insere (TpPilhaM2 &PM, int Elem, int NrP)
  26. {
  27. PM.Pilhas[PM.Topo[NrP - 1]++] = Elem;
  28. }
  29. int Retira (TpPilhaM2 &PM, int NrP)
  30. {
  31. return PM.Pilhas[PM.Topo[NrP - 1]--];
  32. }
  33. int RetornaElemTopo (TpPilhaM2 PM, int NrP)
  34. {
  35. return PM.Pilhas[PM.Topo[NrP - 1]];
  36. }
  37. void Exibe (TpPilhaM2 PM, int NrP)
  38. {
  39. while(!Vazia(PM, NrP))
  40. printf("%d",Retira(PM, NrP));
  41. }

comments powered by Disqus