Advertisement
Guest User

Ps2Ex2

a guest
Dec 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #define Max 1000
  3.  
  4. int vazia (char pilha[], int topo) {
  5.     if (topo == -1)
  6.         return 1;
  7.     else
  8.         return 0;
  9. }
  10. int cheia (int topo) {
  11.     if (topo == Max -1)
  12.         return 1;
  13.     else
  14.         return 0;
  15. }
  16. void empilhar (char pilha[], int *topo, char elemento) {
  17.     if (cheia (*topo)) {
  18.         printf ("PILHA CHEIA, não foi possível empilhar o elemento.");
  19.         return;
  20.     }
  21.     pilha[*topo + 1] = elemento;
  22.     *topo = *topo + 1;
  23.     return;
  24. }
  25. char desempilhar (char pilha[], int *topo) {
  26.     char elemento;
  27.     if (vazia (pilha, *topo)) {
  28.         printf ("Pilha vazia!");
  29.         return 0;
  30.     }
  31.     elemento = pilha[*topo];
  32.     *topo = *topo - 1;
  33.     return elemento;
  34. }
  35. int main() {
  36.     char elementos[1000], pilha[1000];
  37.     int i, topo = - 1, j;
  38.     fgets (elementos, 100, stdin);
  39.     for (i = 0; elementos[i] != '\n' && elementos[i] != '\0'; i++) {
  40.         empilhar (pilha, &topo, elementos[i]);
  41.     }
  42.  for (j = (i - 1); j >= 0; j--) {
  43.         printf ("%c", desempilhar (pilha, &topo));
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement