This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercicio_pilha_7.c
103 lines (89 loc) · 1.71 KB
/
exercicio_pilha_7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool int
#define true 1
#define false 0
#define MAX 40
typedef struct stack{
char str[MAX];
int bottom;
int top;
int devide;
}stack;
bool empty(stack *s);
bool full (stack *s);
void push (stack *s, char *c);
void pop (stack *s, char *c);
void listar(stack *s);
bool verifica_palindromo(stack *s);
int main(){
stack *S = (stack*) malloc(sizeof(stack));
char aux;
printf("Digite a palavra para verificar se é um palindromo. ");
while(scanf("%c",&aux) && aux != '\n'){
push(S,&aux);
}
if(verifica_palindromo(S)){
printf("A palavra é um palíndromo. ");
}
else{
printf("A palavra não é um palindromo. ");
}
free(S);
return 0;
}
bool verifica_palindromo(stack *s){
stack *s_aux = (stack*) malloc (sizeof(stack));
stack *invertido = (stack*) malloc (sizeof(stack));
char aux,aux2;
while(!empty(s)){
pop(s,&aux);//preciso liberar essa pilha pra poder ter ela invertida
push(s_aux,&aux);
push(invertido,&aux);
}
while(!empty(s_aux)){//preencho s de novo com o conteudo armazenado em s_aux;
pop(s_aux,&aux);
push(s,&aux);
}
while(!empty(s) && !empty(invertido)){
pop(s,&aux);
pop(invertido,&aux2);
if(aux!=aux2){
return false;
}
}
free(s_aux);
free(invertido);
return true;
}
void listar(stack *s){
char aux;
while(!empty(s)){
pop(s,&aux);
printf("%c",aux);
}
}
void pop (stack *s, char *c){
if(empty(s)){
printf("EMPTY STACK \n");
return;
}
s->top--;
*c = s->str[s->top];
}
void push (stack *s, char *c){
if(full(s)){
printf("STACK FULL. EXIT REQUIRED \n");
exit(1);
return;
}
s->str[s->top] = *c;
s->top++;
}
bool empty(stack *s){
return s->top == 0;
}
bool full (stack *s){
return s->top == MAX;
}