Stack insertion vector returning all zero

Asked

Viewed 83 times

0

These days I posted the algorithm of this address below here because I was having problems to pop and remove was solved so far. Destamping Dynamic Stack

But as I was reviewing the code and therefore leaving them better I decided to put a for so that the user does not keep typing 1 in the menu to enter the numbers, then he would do the insertion according to the size of the vector:

So I did it below:

   if(op == 1)
    {
     for(i=0;i<=9;i++)
     {
      std::cout << "\n\tINSIRA NUMERO "<<i+1<<" NA PILHA: ";
       pilha *novo = new pilha();
       std::cin >> novo->num[i];
       novo->prox = topo;
       topo = novo;
      std::cout << "\n\tNUMERO "<<novo->num[i]<<" INSERIDO COM SUCESSO!!!";
     }
    }

But when I type 2 it returns everything zero. what I did wrong?

   if(op == 2)
    {
     i++; 
     if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
     else
      {
       std::cout << "\n\tPILHA COMPLETA: ";
       aux = topo;
       while(aux != NULL)
       {
        std::cout << aux->num[i] << " ";
        aux = aux->prox;
       }
      }
    }
  • How the structure/class has been defined pilha ?

  • is the same link algorithm that I put here only changed the int value in a; to int in a[10];

  • i switched to a[10]; in mine here at base is the same algorithm ...

  • If each stack element now has a vector with 10 numbers it is necessary to show them all in each iteration of its while, starting from 0 to 9. As has just shows a number per stack element.

  • then I tried to do while(aux->num[i] <=10) but errors

  • But he’s trying to insert several elements into the stack in a row for is this ? Because to do this with the code you have does not need the vector that ends up complicating (although also give to do)

  • then I tried to do this without using vector, as it was I would have to access the menu if I wanted to insert 10 elements then I should choose 1 and then insert, and when doing this gave segmentation failure without defining one as vector..

Show 2 more comments

1 answer

1


If you want to add multiple elements in a row to the stack at the expense of one for does not need to transform each cell element into an array, and can maintain the old structure it had:

struct pilha {
    int num; //sem ser vetor
    pilha *prox;
};

Changing the insertion to:

for(i=0; i<=9; i++) {
    std::cout << "\n\tINSIRA NUMERO "<<i+1<<" NA PILHA: ";
    pilha *novo = new pilha();
    std::cin >> novo->num; //sem o ->num[i]
    novo->prox = topo;
    topo = novo;
    std::cout << "\n\tNUMERO "<<novo->num<<" INSERIDO COM SUCESSO!!!";//tambem sem num[i]
}

The very one for already makes all the insertions you want, depending on the number of times you set it to execute.

The reading would have to be adjusted to:

if(op == 2) {
    //sem i++;
    if(topo == NULL)std::cout << "\n\tPILHA VAZIA!!!";
    else {
        std::cout << "\n\tPILHA COMPLETA: ";
        aux = topo;
        while(aux != NULL) {
            std::cout << aux->num << " "; //sem ->num[i]
            aux = aux->prox;
        }
    }
}

The problem that it had is that each element of the stack had 10 numbers, but you were just filling the one by leaving the others 9 empty. Analyzing the for who had:

for(i=0;i<=9;i++)
 {
   ...
   pilha *novo = new pilha(); //criar um novo elemento para a pilha
   std::cin >> novo->num[i]; //preencher um numero apenas numa posição do vetor
   ...
 }

Notice how you fill only one position as each iteration creates a new one pilha.

  • Now I understand, I also changed the algorithm to not create several variables https://pastebin.com/UTLsSBqE

Browser other questions tagged

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