Destamping Dynamic Stack

Asked

Viewed 637 times

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.

  1. How do I fix it so that it removes a single element while disemplihar?

  2. 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;
}

1 answer

2


The whole problem is in the way you are "popping" the stack:

aux = topo->prox;

 while(aux != NULL)
{
   aux = aux->prox;
   topo->prox--;
}

Here are two points to highlight.

  1. It doesn’t seem to make sense to remove all the elements without releasing the memory.
  2. You can’t decrement the pointer prox (or do anything like that), because in this case it is a dynastic pile. Decreasing and incrementing only works with vectors, because in the case of vectors, the array of elements in memory is continuous. In the case of dynamomic cells (or lists) it is not certain which position of the next or previous element.

Code suggestion:

aux = NULL;
if(topo->prox){
   if(topo->prox->prox)
      aux = topo->prox->prox;
   std::cout << "Item " << top->prox << "destruido com sucesso.\n";
   delete topo->prox;
   topo->prox = aux;
} else std::cout << "Item não destruido.\n";
  • it still didn’t work out I did that it empties the pile.

  • but the intention is to do what?

  • 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

  • I’ll do the editing

  • now it worked but my doubt is which part I use to show: Std::Cout<"NUMBER: "<<4<<<" SUCCESSFULLY REMOVED."; ?

  • conssegui do here : stack *removed = new stack(); removed = top->Prox; Std::Cout << " n tNUMERO: " << removed->num << "SUCCESSFULLY REMOVED!!!";

  • 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

  • my whim at the time I wrote here in the code is not so I just created the pointer same...

Show 3 more comments

Browser other questions tagged

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