How to compare two chained lists in C?

Asked

Viewed 1,519 times

0

Guys, I have a problem with this issue:

-I have to compare two chained lists and return 1 if they are equal or 0 if they are not.

The function is like this and I can’t progress:

  struct Node
  {
 int data;
 struct Node *next;
 }





 int CompareLists(Node *headA, Node* headB) 
 { 
  /*se as duas forem vazias*/
   if(headA->next==NULL && headB->next==NULL) 
  { 
   return 1; 
   } 
   if(headA->next==NULL || headB->next==NULL) 
   { 
   return 0; 
   }  
   while(headA->next!=NULL && headB->next!=NULL) 
   { 
   if(headA->data=!headB->data) 
   { 
   return 0; 
   }else{ 
   return 1; 
   } 
   headA=headA->next; 
   headB=headB->next; 

   } 
   }

I think the problem is going through the list (headA=headA->next), but I don’t know how to do it. Someone can help me?

  • 1

    Equally, you define the same content in the same order?

  • 1

    Do not update the question code with answer code, as this invalidates any answers given so far, and is not the way Stackoverflow works.

  • @Isac already got a lot of thanks. Sorry for the inconvenience.

2 answers

1

To compare lists you need to go through the lists until you reach a conclusion. This while will only end when at least one of them reaches NULL (which will be tested at while) soon:

while(headA->next != NULL && headB->next != NULL){
    ...
    headA = headA->next;
    headB = headB->next;
}

These are the last lines, ie when headA is in the penultimate structure (ie headA->next or NULL), the while will not be performed because headA reached the penultimate and headB no:

while(0 && 1) => while(0)

I mean, iteration’s over. When iteration is over, the function gives no answer, it simply exits because the only outputs the function has are if’s

int CompareLists(Node *headA, Node* headB) 
{ 
   /*se as duas forem vazias*/
   if(headA->next==NULL && headB->next==NULL) 
   { 
       return 1; 
   } 
   if(headA->next==NULL || headB->next==NULL) 
   { 
       return 0; 
   }  
   while(headA->next!=NULL && headB->next!=NULL) 
   { 
       if(headA->data=!headB->data) 
       { 
           return 0; 
       }else{ 
           return 1; 
       } 
       headA=headA->next; 
       headB=headB->next; 

   } 
}
  • I changed the Return 1 and put after the { do while and took the Else, however, the output is different than expected. It is with the output 1 1 1 1 and has to return 1 0 0 according to what is in the exercise.

  • 2

    while with return in the if and in the else ? Your Energy would not before be out of the while ? And the =! would not be != ?

  • 1

    what @Isac said is true, =! does not exist... or is == ! or is != In the edit I put my way to test if 2 lists are equal. If you want to test my form to see if it is right, do it. If it is correct, read it and understand it. Send me a message and I will help you. I don’t have much experience with lists, but I will do my best to help you.

  • Thanks for the personal observation, but it keeps giving error. Is there any condition? I will edit the code there above

  • 1

    @YODA the first iteration (within while) will give 1 due to Else

  • @Darkgv now changed the code of a look up there.

  • @YODA meta at end of function Return 1 and test sff

Show 2 more comments

1

#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct No
{
    int nConteudo;
    struct No *prox;
}TNo;
 

typedef TNo* TLista;

void liberar_memoria(TLista p);

void verificaPar(TLista p1, TLista p2);

void mostraLista(TLista p1, TLista p2);
 
int main(void)
{
    TLista pNo1 = NULL;
    TLista pNo2 = NULL;
    TLista pNo3 = NULL;
    
    TLista pNo4 = NULL;
    TLista pNo5 = NULL;
    TLista pNo6 = NULL;
     
    
    pNo1 = (TLista) malloc(sizeof(TLista));
    pNo1->nConteudo = 110;
     
    pNo2 = (TLista) malloc(sizeof(TLista));
    pNo2->nConteudo = 210;
     
    pNo3 = (TLista) malloc(sizeof(TLista));
    pNo3->nConteudo = 310;
    
    
    pNo4 = (TLista) malloc(sizeof(TLista));
    pNo4->nConteudo = 110;
     
    pNo5 = (TLista) malloc(sizeof(TLista));
    pNo5->nConteudo = 210;
     
    pNo6 = (TLista) malloc(sizeof(TLista));
    pNo6->nConteudo = 310;
    
    pNo1->prox = pNo2;
    pNo2->prox = pNo3;  
    pNo3->prox = NULL;
    
    pNo4->prox = pNo5;
    pNo5->prox = pNo6;  
    pNo6->prox = NULL;
     
    mostraLista(pNo1,pNo4);
     
    verificaPar(pNo1, pNo4);
    
    liberar_memoria(pNo1);
    liberar_memoria(pNo4);
  
    return 0;
}

void liberar_memoria(TLista p)
{
    TLista atual = p;

    while (atual != NULL)
    {
        TLista proximo = atual->prox;

        free(atual);

        atual = proximo;
    }
}

void mostraLista(TLista p1, TLista p2)
{
    TLista i,j;
    printf("\nMostrando a lista:\n"); 

    if((p1)&&(p2))

    for (i=p1,j=p2; p1!=NULL;p2=p2->prox,p1=p1->prox)
    {
        printf("\n%d\t", i->nConteudo); printf("- %d\n", j->nConteudo);

        i=i->prox;  j=j->prox;        
    }

    else

    printf("Lista vazia.");

    printf("\n\n"); 
}

void verificaPar(TLista p1, TLista p2)
{
    TLista i,j, cont=0;
    
    if((p1)&&(p2))
    {
        for (i=p1,j=p2; p1!=NULL;p2=p2->prox,p1=p1->prox)
        {
            if (i->nConteudo!=j->nConteudo)
            {
                printf("NAO SAO IGUAIS - LISTAS DIFERENTES!\n");
                    cont=0;
                        break;
            }
            else 
            {
                cont++;
            }
            
            i=i->prox;
            
            j=j->prox;          
        }
            if (cont)
            {
                printf("AS LISTAS SAO IDENTICAS\n");
            }
    }    
    
    else
        
    printf("LISTAS IDENTICAS PORQUE AMBAS SAO VAZIAS.");
}

Browser other questions tagged

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