0
I have a dynamic stack algorithm. Only if I insert 10 elements into the stack and pop without removing the first element it from the segmentation failure. And if I remove the first element from the top remaining 9 8 7 6 5 4 3 2 1 and then pop it, it removes 4 elements instead of removing one, so 9 4 3 2 1.
How do I fix it so that it removes a single element while disemplihar?
And how do I insert 10 elements into the stack and pop without giving segmentation failure?
The code:
#include <iostream>
struct pilha
{
int num;
pilha *prox;
};
int main()
{
pilha *topo = NULL;
pilha *aux;
int op;
do
{
std::cout << "\n\n\tPILHA DESEMPLILHA COMO ESTRUTURA DINAMICA"
"\n\tMENU DE ESCOLHA DA PILHA"
"\n\t1 - INSERIR NA PILHA"
"\n\t2 - CONSULTAR TODA PILHA"
"\n\t3 - REMOVER DA PILHA"
"\n\t4 - DESEMPILHAR A PILIHA"
"\n\t5 - ESVAZIAR A PILHA"
"\n\t6 - SAIR"
"\n\tESCOLHA: ";
std::cin >> op;
if(op == 1)
{
std::cout << "\n\tINSIRA NUMERO NA PILHA: ";
pilha *novo = new pilha();
std::cin >> novo->num;
novo->prox = topo;
topo = novo;
std::cout << "\n\tNUMERO "<<novo->num<<" INSERIDO COM SUCESSO!!!";
}
if(op == 2)
{
if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
else
{
std::cout << "\n\tPILHA COMPLETA: ";
aux = topo;
while(aux != NULL)
{
std::cout << aux->num << " ";
aux = aux->prox;
}
}
}
if(op == 3)
{
if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
else
{
aux = topo;
std::cout << "\n\tNUMERO: " << topo->num << " REMOVIDO COM SUCESSO!!!";
topo = topo->prox;
delete(aux);
}
}
if(op == 4)
{
if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
else
{
std::cout<<"\n\tTOPO: "<<topo->num;
aux = topo->prox;
while(aux != NULL)
{
aux = aux->prox;
topo->prox--;
}
}
}
if(op == 5)
{
if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
else
{
aux = topo;
while(aux != NULL)
{
topo = topo->prox;
delete(aux);
aux = topo;
}
std::cout << "\n\tPILHA ESVAZIADA COM SUCESSO!!!!";
}
}
if(op<1 || op>6)std::cout << "\n\tOPCAO INVALIDA!!!";
else
if(op == 6)std::cout<<"\n\tGOOD BYE ...!!\n\n";
}while(op != 6);
return 0;
}
it still didn’t work out I did that it empties the pile.
– dark777
but the intention is to do what?
– user72726
lift the top remove the next hold the top and all numbers after the next. so if I have 1 2 3 4 5 the top is 5 then I would have to raise the 5 remove the 4 and when I type the 2 it would show me 1 2 3 5
– dark777
I’ll do the editing
– user72726
now it worked but my doubt is which part I use to show: Std::Cout<"NUMBER: "<<4<<<" SUCCESSFULLY REMOVED."; ?
– dark777
conssegui do here : stack *removed = new stack(); removed = top->Prox; Std::Cout << " n tNUMERO: " << removed->num << "SUCCESSFULLY REMOVED!!!";
– dark777
Okay, but I don’t understand why to re-allocate the memory (removed = new stack()), if you end up making the pointer point elsewhere. I have not been testing, and it seems that there is an error in my code, is Std::Cout << "Item " << top->Prox->num
– user72726
my whim at the time I wrote here in the code is not so I just created the pointer same...
– dark777