The errors that prevent your program from working are:
-
Instead of frase != 'frase[i] != 'empilha(frase, topo);
'
'
you should have used empilha(frase[i], topo);
.
-
Instead of gets
, you should use goto
In the meantime, I'll still make other important remarks about your code:
-
It's much easier to read, understand, and find problems in a well-coded code.
-
Never, never under any circumstances, use the gets
function. This function should never have been invented and is universally and universally hated by all C programmers in the world. It is more hated than gets
. Whenever you use gets
, a demon coming directly from the depths of hell is invoked on the mortal plane to bring pain, curse, and torment to your program C. Even if the sea boils, if the stars fall, if the sun goes down or if the world ends, still, do not use the fgets
function. The gets(frase);
function has been removed from the default C language in 2011 forever and never to return. It's late already. Use fgets(frase, 50, stdin);
instead.
Instead of topo->conteudo
, use conteudo
.
With these changes, your program looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
void empilha(char c, celula *topo) {
celula *nova = malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = topo->prox;
topo->prox = nova;
}
char desempilha(celula *topo) {
celula *pt = topo->prox;
char c = pt->conteudo;
topo->prox = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
celula cabeca;
celula *topo = &cabeca;
topo->prox = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
for (int i = 0; frase[i] != '#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
typedef struct pilha {
struct cel *cabeca;
} pilha;
void empilha(char c, pilha *p) {
celula *nova = malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = p->cabeca;
p->cabeca = nova;
}
char desempilha(pilha *p) {
celula *pt = p->cabeca;
char c = pt->conteudo;
p->cabeca = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
pilha p;
p.cabeca = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
for (int i = 0; frase[i] != 'celula *nova = malloc(sizeof(celula));
'; i++) {
empilha(frase[i], &p);
}
printf("\nInvertida: ");
while (p.cabeca != NULL) {
putchar(desempilha(&p));
}
putchar('\n');
return 0;
}
'; i++) {
empilha(frase[i], topo);
}
printf("\nInvertida: ");
while (topo->prox != NULL) {
putchar(desempilha(topo));
}
putchar('\n');
return 0;
}
See here working on ideone.
However, what you are using is not exactly a stack. The data structure you have created has a head that is a special knot. A stack implemented as a simply linked list, by the pure concept of the definition of the data structure should not have any knot with special treatment different from the others. Proof of this is that %code% should not be used and the %code% field only makes sense from the second node. This can be solved by giving a different structure to the head, thus getting the code:
celula *nova = (celula *) malloc(sizeof(celula));
See here working on ideone.
Finally, note that C ++ is a bit more demanding than C in casts , so this:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
typedef struct pilha {
struct cel *cabeca;
} pilha;
void empilha(char c, pilha *p) {
celula *nova = (celula *) malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = p->cabeca;
p->cabeca = nova;
}
char desempilha(pilha *p) {
celula *pt = p->cabeca;
char c = pt->conteudo;
p->cabeca = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
pilha p;
p.cabeca = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
printf("\nInvertida: ");
for (int i = 0; frase[i] != '#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
void empilha(char c, celula *topo) {
celula *nova = malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = topo->prox;
topo->prox = nova;
}
char desempilha(celula *topo) {
celula *pt = topo->prox;
char c = pt->conteudo;
topo->prox = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
celula cabeca;
celula *topo = &cabeca;
topo->prox = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
for (int i = 0; frase[i] != '#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
typedef struct pilha {
struct cel *cabeca;
} pilha;
void empilha(char c, pilha *p) {
celula *nova = malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = p->cabeca;
p->cabeca = nova;
}
char desempilha(pilha *p) {
celula *pt = p->cabeca;
char c = pt->conteudo;
p->cabeca = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
pilha p;
p.cabeca = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
for (int i = 0; frase[i] != 'celula *nova = malloc(sizeof(celula));
'; i++) {
empilha(frase[i], &p);
}
printf("\nInvertida: ");
while (p.cabeca != NULL) {
putchar(desempilha(&p));
}
putchar('\n');
return 0;
}
'; i++) {
empilha(frase[i], topo);
}
printf("\nInvertida: ");
while (topo->prox != NULL) {
putchar(desempilha(topo));
}
putchar('\n');
return 0;
}
'; i++) {
for (; frase[i] != 'celula *nova = (celula *) malloc(sizeof(celula));
' && frase[i] != ' '; i++) {
empilha(frase[i], &p);
}
while (p.cabeca != NULL) {
putchar(desempilha(&p));
}
putchar(frase[i] == ' ' ? ' ' : '\n');
}
return 0;
}
In C ++ it should look like this:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct cel {
char conteudo;
struct cel *prox;
} celula;
typedef struct pilha {
struct cel *cabeca;
} pilha;
void empilha(char c, pilha *p) {
celula *nova = (celula *) malloc(sizeof(celula));
nova->conteudo = c;
nova->prox = p->cabeca;
p->cabeca = nova;
}
char desempilha(pilha *p) {
celula *pt = p->cabeca;
char c = pt->conteudo;
p->cabeca = pt->prox;
free(pt);
return c;
}
int main() {
char frase[50];
pilha p;
p.cabeca = NULL;
printf("Informe a frase: ");
fgets(frase, 50, stdin);
printf("\nInvertida: ");
for (int i = 0; frase[i] != '%pre%'; i++) {
for (; frase[i] != '%pre%' && frase[i] != ' '; i++) {
empilha(frase[i], &p);
}
while (p.cabeca != NULL) {
putchar(desempilha(&p));
}
putchar(frase[i] == ' ' ? ' ' : '\n');
}
return 0;
}
And considering that you just want to reverse the words, but not the order of them in the sentence, you will need another loop. Your code looks like this:
%pre%
See here working on ideone.