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
)– Gabriel
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.
– anonimo