Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct No{
- char dado;
- struct No *proximo;
- };
- struct Pilha{
- int tamanho;
- struct No *cabeca;
- };
- void iniciaPilha(struct Pilha *pilha){
- pilha->tamanho = 0;
- pilha->cabeca = NULL;
- }
- int tamanhoPilha(struct Pilha *pilha){
- return pilha->tamanho;
- }
- int vaziaPilha(struct Pilha *pilha){
- if(pilha->tamanho == 0){
- return 1;
- }else{
- return 0;
- }
- }
- char topoPilha(struct Pilha *pilha){
- if(vaziaPilha(pilha)) return '0';
- return (pilha->cabeca)->dado;
- }
- int push(struct Pilha *pilha, char caracter){
- struct No *novoNo;
- novoNo = malloc(sizeof(struct No));
- if(novoNo == NULL) return 0;
- novoNo->dado = caracter;
- novoNo->proximo = pilha->cabeca;
- pilha->cabeca = novoNo;
- pilha->tamanho++;
- return 1;
- }
- char pop(struct Pilha *pilha){
- struct No *aux = pilha->cabeca;
- char caracter;
- if(vaziaPilha(pilha)) return '0';
- caracter = (pilha->cabeca)->dado;
- pilha->cabeca = aux->proximo;
- free(aux);
- pilha->tamanho--;
- return caracter;
- }
- int main(){
- char string[1001], string_invertida[1001];
- struct Pilha pilha;
- iniciaPilha(&pilha);
- scanf("%1000[^\n]", string);
- int i, tam = strlen(string);
- for(i = 0; i < tam; i++){
- push(&pilha, string[i]);
- }
- for(i = 0; i < tam; i++){
- string_invertida[i] = pop(&pilha);
- }
- string_invertida[i] = '\0';
- if(strcmp(string, string_invertida) == 0){
- printf("A palavra/frase %s é palindromo.\n", string);
- }else{
- printf("A palavra/frase %s não é palindromo.\n", string);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment