Created
March 12, 2020 02:48
-
-
Save TopRoupi/159e55d3a88935d2d20871b3afaef7ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define TAM 10 | |
int main(void) { | |
typedef struct pilha{ | |
char vet[TAM]; | |
int topo; | |
} tpilha; | |
void create(tpilha *p){ | |
p->topo = -1; | |
} | |
void destroy(tpilha *p){ | |
p->topo = -1; | |
} | |
int is_empty(tpilha p){ | |
return(p.topo == -1); | |
} | |
int is_full(tpilha p){ | |
return(p.topo == TAM); | |
} | |
char top(tpilha p){ | |
return(p.vet[p.topo]); | |
} | |
void push(tpilha *p, char elem){ | |
if(is_full(*p)){ | |
printf("overflow"); | |
exit(1); | |
} | |
else { | |
p->vet[++p->topo] = elem; | |
} | |
} | |
char pop(tpilha *p){ | |
if(is_empty((*p))){ | |
printf("underflow"); | |
exit(1); | |
} | |
else{ | |
return(p->vet[p->topo--]); | |
} | |
} | |
int palindroma(tpilha p){ | |
tpilha aux; | |
create(&aux); | |
int tam_p = p.topo +1; | |
for(int i = 0; i < tam_p / 2; i++) | |
push(&aux, pop(&p)); | |
if(tam_p % 2 == 1) pop(&p); | |
for(int i = 0; !is_empty(p); i++) | |
if(pop(&p) != pop(&aux)) return 0; | |
return 1; | |
} | |
void inverter_palavra_a_palavra(tpilha *p){ | |
tpilha aux; | |
create(&aux); | |
tpilha inversa; | |
create(&inversa); | |
for(; !is_empty(*p);) | |
push(&inversa, pop(&(*p))); | |
for(; !is_empty(inversa);){ | |
if(top(inversa) == ' '){ | |
pop(&inversa); | |
for(; !is_empty(aux);) | |
push(&(*p), pop(&aux)); | |
push(&(*p), ' '); | |
} | |
push(&aux, pop(&inversa)); | |
} | |
for(; !is_empty(aux);) | |
push(&(*p), pop(&aux)); | |
} | |
//primeiro exemplo | |
tpilha pilha; | |
create(&pilha); | |
push(&pilha, 'a'); | |
push(&pilha, 'b'); | |
push(&pilha, 'c'); | |
push(&pilha, ' '); | |
push(&pilha, 'e'); | |
push(&pilha, 'e'); | |
push(&pilha, ' '); | |
push(&pilha, 'c'); | |
push(&pilha, 'b'); | |
push(&pilha, 'a'); | |
push(&pilha, '\0'); | |
printf(pilha.vet); | |
pop(&pilha); | |
printf("\n"); | |
if(palindroma(pilha)) printf("palindroma\n"); | |
inverter_palavra_a_palavra(&pilha); | |
push(&pilha, '\0'); | |
printf(pilha.vet); | |
pop(&pilha); | |
printf("\n"); | |
printf("\n"); | |
printf("\n"); | |
printf("\n"); | |
//segundo exemplo | |
tpilha pilha2; | |
create(&pilha2); | |
push(&pilha2, 'a'); | |
push(&pilha2, 'b'); | |
push(&pilha2, 'c'); | |
push(&pilha2, ' '); | |
push(&pilha2, 'd'); | |
push(&pilha2, 'e'); | |
push(&pilha2, ' '); | |
push(&pilha2, 'f'); | |
push(&pilha2, 'g'); | |
push(&pilha2, 'h'); | |
push(&pilha2, '\0'); | |
printf(pilha2.vet); | |
pop(&pilha2); | |
printf("\n"); | |
if(palindroma(pilha2)) printf("palindroma\n"); | |
inverter_palavra_a_palavra(&pilha2); | |
push(&pilha2, '\0'); | |
printf(pilha2.vet); | |
pop(&pilha2); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment