Use of batteries and passage by reference

Asked

Viewed 57 times

0

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

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 someone help me understand why the original battery isn’t being altered or give me a hint how to fix it? Thank you

1 answer

1

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

Apparently you are trying to copy "pilha2" to "P" at the end of the function.

That’s not working, first because "P" is a parameter, so it’s a local variable to the function, second because that would only make sense if the cells had been allocated in the heap, which doesn’t seem to be the case, and even then, in this case the original cell would need to be displaced.

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);     

Browser other questions tagged

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