Compare two chained lists and return whether they are equal or different

Asked

Viewed 1,406 times

1

I’ve been trying to compare two lists simply chained and return if they are equal or different but I’m not able to match the compare function, it only compares the last value inserted whether it is equal or not, if I change the others appears that everything is the same!

 void Comparar(no *l1, no *l2) 
 { 
    while (l1->prox!= NULL){
    l1= l1;
     while(l2->prox!= NULL){
        l2= l2;
          if(l1->info == l2->info){

                 printf("Iguais\n\n");
          } else
                  printf("Diferentes\n\n");
    system("pause");    
    }     

}

}  

The complete program is this:


#include <stdio.h>
#include <stdlib.h>
typedef struct no {
  int info;
  struct no* prox;
}no;



no* cria_no (void) {
  return NULL;
}



no* inserir (no* lst, int v) {
  no* novo = (no*) malloc(sizeof(no));
  novo->info = v;
  novo->prox = lst;
  return novo;
}


 void Comparar(no *l1, no *l2) 
 { 
    while (l1->prox!= NULL){
    l1= l1;
     while(l2->prox!= NULL){
        l2= l2;
          if(l1->info == l2->info){

                 printf("Iguais\n\n");
          } else
                  printf("Diferentes\n\n");
    system("pause");    
    }     

}

}
int main (void)
{
  no* lst1; 
  no* lst2; 

  lst1 = cria_no(); 
  lst1 = inserir(lst1, 21); 
  lst1 = inserir(lst1, 45); 
  lst1 = inserir(lst1, 10); 

  lst2 = cria_no(); 
  lst2 = inserir(lst2, 21); 
  lst2 = inserir(lst2, 45); 
  lst2 = inserir(lst2, 5); 


    printf("\nAS LISTAS SAO:\n");

    Comparar(lst1,lst2);


}

Does anyone know how to solve?

  • Edit the question to add the language tag you’re using (it’s probably c)

  • You have to start from the assumption that they are equal by placing a variable as true, test the equality element by element and if they are different or have different amounts of elements put the variable as false. At the end print whether they are the same or different.

1 answer

0


Some comments on your code

  1. The formatting is not cool. Search the internet for good code organization and indentation practices, and see if your editor has a command to automatically format.
  2. The function main was declared of the type int, but is not returning anything. The header stdlib.h has two constants, a EXIT_SUCCESS and the EXIT_FAILURE, that you can use as return of main. Return EXIT_SUCCESS if the execution of the program has been successful, otherwise EXIT_FAILURE.
  3. Are you using the malloc to allocate memory, but is not using the free to release the allocated memory. You must call the free for all pointers returned by malloc. That is much important.
  4. Whenever you call the malloc you need to check if the return is not NULL. If the malloc return NULL means he couldn’t allocate the requested memory. That’s much important also.
  5. No need to cast in the return of malloc.

Now let’s get to the problem of comparison

Its function Comparar printa message on the screen for each value compared, only that there’s a system("pause"); inside the loop, then it pauses the run after the first comparison.

You must iterate through both lists simultaneously. Return false if two different elements are found or if one of the lists ends before the other, otherwise true.

Full programme:

(No warnings with gcc -Wall -pedantic -std=c11)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define retornar_failure_caso_null(expr)             \
    if ((expr) == NULL)                              \
    {                                                \
        fprintf(stderr, "Falha ao alocar memória!"); \
        return EXIT_FAILURE;                         \
    }

typedef struct no
{
    int info;
    struct no *prox;
} no;

no *cria_no(void)
{
    return NULL;
}

no *inserir(no *lst, int v)
{
    no *novo = malloc(sizeof(no));

    if (novo == NULL)
    {
        return NULL;
    }

    novo->info = v;
    novo->prox = lst;

    return novo;
}

bool comparar(no *lista1, no *lista2)
{
    no *atual1 = lista1;
    no *atual2 = lista2;

    while (atual1 != NULL && atual2 != NULL)
    {
        if (atual1->info != atual2->info)
        {
            return false;
        }

        atual1 = atual1->prox;
        atual2 = atual2->prox;
    }

    return atual1 == NULL && atual2 == NULL;
}

void liberar_memoria(no *lista)
{
    no *atual = lista;

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

        free(atual);

        atual = proximo;
    }
}

int main(void)
{
    no* lista1 = cria_no();

    retornar_failure_caso_null(lista1 = inserir(lista1, 21));
    retornar_failure_caso_null(lista1 = inserir(lista1, 45));
    retornar_failure_caso_null(lista1 = inserir(lista1, 10));

    no* lista2 = cria_no();

    retornar_failure_caso_null(lista2 = inserir(lista2, 21));
    retornar_failure_caso_null(lista2 = inserir(lista2, 45));
    retornar_failure_caso_null(lista2 = inserir(lista2, 5));

    if (comparar(lista1, lista2))
    {
        puts("São iguais");
    }
    else
    {
        puts("São diferentes");
    }

    liberar_memoria(lista1);
    liberar_memoria(lista2);

    return EXIT_SUCCESS;
}
  • Thanks for the information, helped a lot with the problem and I’ll give a study to improve my formatting. I focused a lot on comparing and forgot all the rest rs

Browser other questions tagged

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