i need a function that checks in two linked lists are equal thi is what i have so far

bool equal(list L1 , list L2 , int (*p_cmp_f)() ){

if( L1==NULL && L2==NULL)
return TRUE;
else if( L1==NULL || L2==NULL)
return FALSE;
else if( (*p_cmp_f)(L1->data,L2->data) == -1)
return FALSE;
else
equal(L1->next,L2->next) ;

return TRUE;
}

so this is basically how i define a node

struct node { generic_ptr datapointer; list next; };
i basically define a list as a pointer to a node 

so a node has a datapointer field and a next field which is basically pointing to another node since a list is a pointer to a node

so the first time im passing the two lists and a compare function
here is the compare function = 

int compare_int( generic_ptr x , generic_ptr y){

if( *(int*) x < *(int *)y ) return -1 ; 
if( *(int*) x > *(int *)y ) return 1 ; 
return 0 ; 

}

Edited 5 Years Ago by rockerjhr: n/a

After 15 posts, you should know to ALWAYS use code tags around your code.

I'm always a bit amazed by posts like this. You have your program, why not test it? It's VERY difficult to look at code and see if it's got a bug or not in it. An obvious flaw - sure, but lots of times there will be errors that only a test run will catch.

this is the func :

bool equal(list L1 , list L2 , int (*p_cmp_f)() ){

if( L1==NULL && L2==NULL)
return TRUE;
else if( L1==NULL || L2==NULL)
return FALSE;
else if( (*p_cmp_f)(L1->datapointer,L2->datapointer ) == -1)
return FALSE;

else
equal(L1->next,L2->next,p_cmp_f) ;

return TRUE;
}

in in the equal equation you mean int (*p_cmp_f)()


yes since im passing a function to it im passing list one list two and a function to compare them

heres my main file

#include <stdio.h> 
#include <stdlib.h>
#include "globals.h"
#include "list.h" 

status write_int(generic_ptr p_n ){

	printf(" %d " , *(int *)p_n ) ; 
return OK ; 

}

int compare_int( generic_ptr x , generic_ptr y){

	if( *(int*) x < *(int *)y ) return -1 ; 
       if( *(int*) x > *(int *)y ) return  1 ;  
return 0 ; 

}

int main( int argc , char *argv[]){ 

	list L1 , L2 ; 
	int n,num,i, *p_int ; 

	init_list(&L1) ; 
	init_list(&L2) ; 

printf("\nHow many umbers are there in list 1 ? ") ; 
scanf("%d" , &n)  ; 

for(i = 1  ; i <= n ; i++){

	printf("\nEnter a number: " )  ; 
	scanf("%d" , &num) ; 
	p_int = (int *)malloc(sizeof(int) ) ; 
	*p_int = num ; 
	append(&L1,(generic_ptr)p_int) ; 
	
}

printf("\nList 1: ") ; traverse(L1,write_int) ; 

printf("\n\nHow many numbers are there in List 2 ? " ) ; 
scanf("%d" , &n) ; 
 
for(i = 1  ; i <= n ; i++){

	printf("\nEnter a number: " )  ; 
	scanf("%d" , &num) ; 
	p_int = (int *)malloc(sizeof(int) ) ; 
	*p_int = num ; 
	append(&L2,(generic_ptr)p_int) ; 
	
}

printf("\nList 2: ") ; traverse(L2,write_int) ; 

if(equal(L1,L2,compare_int) == TRUE){
printf("\n\nThe two lists are equal.\n\n") ; 

}

 else
printf("\n\nThe two lists are not equal.\n\n") ; 

return 0 ; 

}
This article has been dead for over six months. Start a new discussion instead.