Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define Max 1000
- int vazia (char pilha[], int topo) {
- if (topo == -1)
- return 1;
- else
- return 0;
- }
- int cheia (int topo) {
- if (topo == Max -1)
- return 1;
- else
- return 0;
- }
- void empilhar (char pilha[], int *topo, char elemento) {
- if (cheia (*topo)) {
- printf ("PILHA CHEIA, não foi possível empilhar o elemento.");
- return;
- }
- pilha[*topo + 1] = elemento;
- *topo = *topo + 1;
- return;
- }
- char desempilhar (char pilha[], int *topo) {
- char elemento;
- if (vazia (pilha, *topo)) {
- printf ("Pilha vazia!");
- return 0;
- }
- elemento = pilha[*topo];
- *topo = *topo - 1;
- return elemento;
- }
- int main() {
- char elementos[1000], pilha[1000];
- int i, topo = - 1, j;
- fgets (elementos, 100, stdin);
- for (i = 0; elementos[i] != '\n' && elementos[i] != '\0'; i++) {
- empilhar (pilha, &topo, elementos[i]);
- }
- for (j = (i - 1); j >= 0; j--) {
- printf ("%c", desempilhar (pilha, &topo));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement