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:
- 5
- And ekrofhfdufd
- D
- And saksajosaksa
- E sajpodfspfdsp
- 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?
– anonimo