Using stack concept to invert words

1

I'm doing a work on Algorithms and Data Structure, which uses the concept of stack to do the inversion of words. However, when compiling the program, it is pointing out some errors that I do not know how to modify them.

Note: I have marked the errors in the program in the form of comments.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct cel {

char conteudo;
struct cel *prox;
} celula;

void empilha(char c, celula *topo) { /* Atenção Aqui*/

celula *nova;
nova = malloc( sizeof (celula)); /* Principal erro do programa!! */
nova->conteudo = c;
nova->prox = topo->prox;
topo->prox = nova;

}

char desempilha(celula *topo) {

char c;
celula *pt;
pt = topo->prox;
c = pt->conteudo;
topo->prox = pt->prox;
free(pt);
return c;

}

int main() {

char frase[50];
int i;
celula cabeca;
celula *topo;
topo = &cabeca; 
topo->prox = NULL;
printf("Informe a frase: ");
gets(frase); /* Possível erro aqui  ()*/
for (i=0; frase != '
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct cel {

char conteudo;
struct cel *prox;
} celula;

void empilha(char c, celula *topo) { /* Atenção Aqui*/

celula *nova;
nova = malloc( sizeof (celula)); /* Principal erro do programa!! */
nova->conteudo = c;
nova->prox = topo->prox;
topo->prox = nova;

}

char desempilha(celula *topo) {

char c;
celula *pt;
pt = topo->prox;
c = pt->conteudo;
topo->prox = pt->prox;
free(pt);
return c;

}

int main() {

char frase[50];
int i;
celula cabeca;
celula *topo;
topo = &cabeca; 
topo->prox = NULL;
printf("Informe a frase: ");
gets(frase); /* Possível erro aqui  ()*/
for (i=0; frase != '%pre%'; i++)
empilha(frase, topo); /* Talvez erro aqui ()*/
printf("\nInvertida: ");
while (topo->prox != NULL)
putchar(desempilha(topo));
putchar('\n');
return 0;

}
'; i++) empilha(frase, topo); /* Talvez erro aqui ()*/ printf("\nInvertida: "); while (topo->prox != NULL) putchar(desempilha(topo)); putchar('\n'); return 0; }
    
asked by anonymous 12.03.2018 / 07:22

1 answer

4

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.

    
12.03.2018 / 08:18