Battery usage and passage by reference

0

Hello, I'm doing a program where I have a stack and want to reorganize the elements of it. I created a function that does this only if the original stack is not being modified (it is only modified in the function but is not modified in memory). Below is the function code and the call of the function.

void organizaPilha(PilhaPostagens *P, Mensagem postagem) {
CelulaMensagem *celulaAuxiliar, *celulaAuxiliar2;
PilhaPostagens pilhaAuxiliar, pilha2;
iniciaPilha(&pilhaAuxiliar);
if(verificaPilhaVazia(P)) {
  puts("Pilha Vazia, não é possivel desempilhar o item");
  return;
}
celulaAuxiliar = P->topo->proximo;
while((celulaAuxiliar->timeline.IDmensagem != postagem.IDmensagem)) {
  empilhaItem(&pilhaAuxiliar, celulaAuxiliar->timeline);
  celulaAuxiliar = celulaAuxiliar->proximo;
}
celulaAuxiliar2 = celulaAuxiliar;
while(celulaAuxiliar->proximo != NULL) {
  empilhaItem(&pilhaAuxiliar, celulaAuxiliar->proximo->timeline);
  celulaAuxiliar = celulaAuxiliar->proximo;
}
iniciaPilha(&pilha2);
celulaAuxiliar = pilhaAuxiliar.topo->proximo;
while(celulaAuxiliar != NULL) {
  empilhaItem(&pilha2, celulaAuxiliar->timeline);
  celulaAuxiliar = celulaAuxiliar->proximo;
}
empilhaItem(&pilha2, celulaAuxiliar2->timeline);
P = &pilha2;
liberaPilha(&pilhaAuxiliar);
liberaPilha(&pilha2);
}

organizaPilha(&postagens[indiceAmigos[auxiliar]], mensagemCurtida);

Can anyone help me understand why the original stack is not being changed or give me a hint how to resolve this? Thanks

    
asked by anonymous 08.10.2016 / 22:28

1 answer

1

The original stack is not being changed because you are putting changes in "stack2".

Apparently you're trying to copy "stack2" to "P" at the end of the function.

This is not working, first because "P" is a parameter, so it is a local variable to the function, second because this would only make sense if the heap had been allocated in the heap, which does not seem to be the case, and even so in this case the original stack would need to be deallocated.

void organizaPilha(PilhaPostagens *P, Mensagem postagem)
{
   CelulaMensagem *celulaAuxiliar, *celulaAuxiliar2;
   PilhaPostagens pilhaAuxiliar /*, pilha2*/;

   iniciaPilha(&pilhaAuxiliar);

   if (verificaPilhaVazia(P))
   {
      puts("Pilha Vazia, não é possivel desempilhar o item");
      return;
   }

   celulaAuxiliar = P->topo->proximo;

   while ((celulaAuxiliar->timeline.IDmensagem != postagem.IDmensagem))
   {
      empilhaItem(&pilhaAuxiliar, celulaAuxiliar->timeline);
      celulaAuxiliar = celulaAuxiliar->proximo;
   }

   celulaAuxiliar2 = celulaAuxiliar;

   while (celulaAuxiliar->proximo != NULL)
   {
      empilhaItem(&pilhaAuxiliar, celulaAuxiliar->proximo->timeline);
      celulaAuxiliar = celulaAuxiliar->proximo;
   }

// iniciaPilha(&pilha2);
   iniciaPilha(P);

   celulaAuxiliar = pilhaAuxiliar.topo->proximo;

   while (celulaAuxiliar != NULL)
   {
//    empilhaItem(&pilha2, celulaAuxiliar->timeline);
      empilhaItem(P, celulaAuxiliar->timeline);
      celulaAuxiliar = celulaAuxiliar->proximo;
   }

// empilhaItem(&pilha2, celulaAuxiliar2->timeline);
   empilhaItem(P, celulaAuxiliar2->timeline);

// P = &pilha2;

   liberaPilha(&pilhaAuxiliar);
// liberaPilha(&pilha2);
}

organizaPilha(&postagens[indiceAmigos[auxiliar]], mensagemCurtida);     
    
08.10.2016 / 23:29