Advertisement
Jonas_3k

/*-(APS) Inverter String usando pilhas -*/

May 19th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. //Definição do registro.
  5. typedef struct pilha{
  6.     char c;
  7.     struct pilha *prox;
  8. }pilha;
  9. //Prototipação das funções a serem utilizadas.
  10. void ler(pilha *p);
  11. void push(pilha **p,char c);
  12. char pop(pilha *p);
  13. void inicia(pilha **p);
  14.  
  15. int main()
  16. {
  17.     int n;
  18.     char s[255];
  19.     pilha *p=NULL, *aux = NULL;
  20.     puts("Entre com uma frase: ");
  21.     scanf("%s",s);
  22.    
  23.     inicia(&p);
  24.     //Carregar a pilha.
  25.     for(n = 0; n<strlen(s); n++)
  26.         push(&p, s[n]);
  27.  
  28.         ler(p);
  29.         puts("");  
  30.         return 0;
  31. }
  32. //função que insere dados.
  33. void push(pilha **p,char c)
  34. {
  35.     if((*p)->c!='.') //Verifica se existe algo na primeira unidade da pilha, caso não tenha passa para o else.
  36.     {
  37.         pilha *aux=NULL;
  38.         inicia(&aux);
  39.         aux->c = c;
  40.         aux->prox = *p;
  41.         *p = aux;
  42.     }
  43.     else
  44.         (*p)->c = c;
  45. }
  46. //retorna o topo da pilha.
  47. char pop(pilha *p)
  48. {
  49.     if(p!=NULL)
  50.         return p->c;
  51. }
  52. //inicia a pilha.
  53. void inicia(pilha **p)
  54. {
  55.     if(*p==NULL){
  56.         *p = (pilha *) malloc(sizeof(pilha));
  57.         (*p)->c = '.';
  58.         (*p)->prox = NULL;
  59.     }
  60.     else puts("Ponteiro pronto para uso");
  61. }
  62. //retorna todos os elementos do topo até o fim da pilha.
  63. void ler(pilha *p)
  64. {
  65.     if(p != NULL)
  66.     {
  67.         putchar(p->c);
  68.         ler(p->prox);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement