Check that stack 1 elements are equal to stack 2, C++

Asked

Viewed 702 times

-1

You guys, good night! I need to do this part of the code in C++ comparing whether the elements on Stack 1 are the same as on Stack 2 and whether the amount of elements are exactly the same. I don’t know how to implement that part of the code. I created the int i variable to go through the list. #Meajudem

int compara_pilha1_com_pilha2 (Pilha* pi1, Pilha* pi2)
{
   int i;

   if(pi1 == NULL || pi1->qtd == 0 || pi2 == NULL || pi2->qtd == 0)
   {
    return 0;
   }
   for(i = 0; i<pi1->qtd; i++)
   {

   }
  • How the structure is defined Pilha ? How the elements were inserted ?

1 answer

0

Making the necessary modifications in cx11++

These modifications should help you:

int compara_pilha1_com_pilha2 (Pilha* pi1, Pilha* pi2)
{
   //verifica se não esta nulo
   if(pi1 == nullptr || pi2 == nullptr)
   {
     return 0;
   }
   //verifica se as duas pilas não estão com quantidade igual a 0 e se 
   //as quantidades são iguais
   if(pi1->qtd == 0 || pi2->qtd == 0 || pi1->qtd != pi2->qtd)
   {
     return 0;
   }
   //para comparar agora falta os campos
   //vamos supor que seja value
   for(auto i = 0; i < pi1->qtd; i++)
   {
      if((pi1 + i)->value != (pi2 + i)->value)
      {
        return 1;
      }
   } 
   return 0;
}

Browser other questions tagged

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