Structure of Data/ Stacks

Asked

Viewed 111 times

0

People need to do a battery exercise that receives String, in which in the first line I have to include the size of the stack and in the others go giving the commands, example :

Entree:

  1. 5
  2. And ekrofhfdufd
  3. D
  4. And saksajosaksa
  5. E sajpodfspfdsp
  6. X

Exit: sajpodfspfdsp saksajosaksa

The Commands are as follows: :

  • And- stack

  • D- pop

  • X- printout from top to bottom
  • B- printar base to top
  • T- print the value from the top of the list

I tried to do something but giving you several mistakes of char, someone could give me a light on how to proceed?
Follow what I’ve managed to do:

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

struct Pilha
{
    char *itens, val;
    int topo, tamanho;

};

struct Pilha criaPilha(int);
void empilha(struct Pilha *, char);
int desempilha(struct Pilha *);
int vazia(struct Pilha);
int cheia(struct Pilha);
void topoBase(struct Pilha);
void baseTopo(struct Pilha);
void exibetopo(struct Pilha);

int main()
{
    int n, val;
    char op;
    struct Pilha p;

    scanf("%d\n", &n);
    p = criaPilha(n);

    while(scanf("%c\n", &op) != EOF)
    {
        if(op == 'E')
        {
            scanf("%s\n", val);
            empilha(&p, val);
        }
        else if(op == 'D')
        {
            desempilha(&p);
        }
        else if(op == 'X')
        {
            topoBase(p);
        }
        else if(op == 'B')
        {
            baseTopo(p);
        }

        else
        {
            exibetopo(p);
        }
    }
    return 0;
}

struct Pilha criaPilha(int n)
{
    struct Pilha p;
    p.itens =malloc(n * sizeof(char));
    p.topo = -1;
    p.tamanho = n;
    return p;
}

void empilha(struct Pilha *p, char novo)
{
    if(!cheia(*p))
    {
        p->itens[++p->topo] = novo;
    }
}

char desempilha(struct Pilha *p)
{
    char ret;
    if(!vazia(*p))
    {
        ret = p->itens[p->topo--];
        return ret;
    }
}

char vazia(struct Pilha p)
{
    return p.topo == -1;
}

char cheia(struct Pilha p)
{
    return p.topo == p.tamanho-1;
}

void baseTopo(struct Pilha p)
{
    int i;
    for(i=0; i<=p.topo; i++)
    {
        printf("%s\n", p.itens[i]);
    }
}

void topoBase(struct Pilha p)
{
    int i;
    for(i=p.topo; i>=0; i--)
    {
        printf("%s\n", p.itens[i]);
    }
}

void exibetopo(struct Pilha p)
{
    i = p.topo;
    printf("%s\n", p.itens[p.topo]);
}
  • Your readings do not agree with what you stated are the entries. The command is followed by a character ' ' or a ' n' and then a string?

1 answer

1

You have some points to fix in the code:

  • You declare in your struct one char* itens and a char val. val is never used, and itens is a hand char, that is, a string. It doesn’t seem your goal. I think what you want there is a char** itens, namely, a char pointer (or a string pointer).

  • The guys int and char are different things in C, you can’t declare a function int desempilha(...) and then implement how char desempilha(...).

  • The guy char is not a string. char is a bad name anyway, understand how byte. I saw there that you use char sometimes trying to refer to strings (the method desempilha, for example, and the char ret there). Whenever you want strings (ie byte sequences), use char* or an array of char (Ex.: char str[256];).

  • Less relevant, but your instruction "E" can’t be written the way you want it. How do you read a char and a \n for the instruction, you will have to write like this:

5
D
E
ekrofhfdufd
E
saksajosaksa
[...]

TL;DR: compilation errors are mostly because you used int where I should have worn char or vice versa. Other than that, you need to tidy up the logic to use char* in strings instead of just char.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.