Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //Definição do registro.
- typedef struct pilha{
- char c;
- struct pilha *prox;
- }pilha;
- //Prototipação das funções a serem utilizadas.
- void ler(pilha *p);
- void push(pilha **p,char c);
- char pop(pilha *p);
- void inicia(pilha **p);
- int main()
- {
- int n;
- char s[255];
- pilha *p=NULL, *aux = NULL;
- puts("Entre com uma frase: ");
- scanf("%s",s);
- inicia(&p);
- //Carregar a pilha.
- for(n = 0; n<strlen(s); n++)
- push(&p, s[n]);
- ler(p);
- puts("");
- return 0;
- }
- //função que insere dados.
- void push(pilha **p,char c)
- {
- if((*p)->c!='.') //Verifica se existe algo na primeira unidade da pilha, caso não tenha passa para o else.
- {
- pilha *aux=NULL;
- inicia(&aux);
- aux->c = c;
- aux->prox = *p;
- *p = aux;
- }
- else
- (*p)->c = c;
- }
- //retorna o topo da pilha.
- char pop(pilha *p)
- {
- if(p!=NULL)
- return p->c;
- }
- //inicia a pilha.
- void inicia(pilha **p)
- {
- if(*p==NULL){
- *p = (pilha *) malloc(sizeof(pilha));
- (*p)->c = '.';
- (*p)->prox = NULL;
- }
- else puts("Ponteiro pronto para uso");
- }
- //retorna todos os elementos do topo até o fim da pilha.
- void ler(pilha *p)
- {
- if(p != NULL)
- {
- putchar(p->c);
- ler(p->prox);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement