Unpacking dynamic stack

0

I have a dynamic stack algorithm. Only if I insert 10 elements into the stack and it is unstackable without removing the first element it from the segmentation fault. And if I remove the first element from the top left over 9 8 7 6 5 4 3 2 1 and then unstack, it removes 4 elements instead of removing only one, then getting 9 4 3 2 1.

  • How do I resolve this so that it removes a single element when un-deploying?

  • 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;
    }
    
        
    asked by anonymous 13.01.2018 / 16:45

    1 answer

    2

    The whole problem lies in how you are "piling up" the stack:

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

    Here are two highlights.

  • It does not seem to make sense to remove all elements without freeing the memory.
  • You can not decrease the prox (or do anything like) pointer, because in this case it is a dynamic stack. Decrement and increment only works with vectors, because in the case of vectors, the arrangement of elements in memory is continuous. In the case of dynamic (or list) stacks, the position of the next element or the previous element is not known.
  • Code hint:

    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";
    
        
    13.01.2018 / 17:53