im trying to write a program to see if two lists are equal

im really kinda lost at the moment, i havent gotten the compare function yet but i think that should be pretty easy, but im having some errors which u can see.

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

typedef struct node{ int num; struct node *next; } NODE;

// having an issue before this equal, L1, and )
bool equal( node L1, node L2, int( *p_cmp_func )() ){

  if( empty_list( L1 ) && empty_list( L2 )) return TRUE;
  if( empty_list( L1 ) && !empty_list( L2 )) return FALSE;
  if( !empty_list( L1 ) && empty_list( L2 )) return FALSE;

  return((*p_cmp_f)(DATA(L1),DATA(L2)) && equal(NEXT(L1), NEXT(L2), p_cmp_f));
}
int main( int argc, char *argv[] ){

  NODE *L1, *L2;
  NODE *temp ;
  int n, i, num;

  printf( "\n How many numbers do you want to enter for the first list? " );
  scanf( "%d", &n );

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

    temp = ( NODE * ) malloc( sizeof( NODE ) );

    printf( "Please enter a number: " );
    scanf( "%d", &num );

    temp -> num = num;
    temp -> next = L1;
    L1 = temp ;
  }
  printf( "\n How many numbers do you want to enter for the second list? " );
  scanf( "%d", &n );

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

    temp = ( NODE * ) malloc( sizeof( NODE ) );

    printf( "Please enter a number: " );
    scanf( "%d", &num );

    temp -> num = num;
    temp -> next = L2;
    L2 = temp;
  }

  printf( "\n\n" );
/* im also having some issues here but i think these are related to not having compare list written, and the previous error*/
  if( equal( L1, L2, comparelist() ) == TRUE )
    printf( "The lists are equal." );

  if( equal( L1, L2, comparelist() ) == FALSE )
    printf( "The lists are not equal." );

  return( 0 );
}

Recommended Answers

All 6 Replies

In equal(), declare L1 and L2 as NODE*, not node.
C does not generally support bool (use int).
TRUE and FALSE need to be declared, or just use 1 and 0.
(Or perhaps your version of C supports bool, in which case you mean true and false, not TRUE and FALSE.)
You spell p_cmp_func as p_cmp_f in the return.
NEXT is not defined anywhere but is used in the return.

By the way, it is customary to mark a thread as solved when it is solved.

will do thanks very much nucleon

I made those changes, i now am including globals.h which contains this:

#ifndef _globals
#define _globals

#define DATA( L ) ( ( L ) -> datapointer )
#define NEXT( L ) ( ( L ) -> next )

typedef enum { OK, ERROR } status ;
typedef enum { FALSE=0 , TRUE=1 } bool ;
typedef void *generic_ptr ;

#endif

now im getting one error about assinging pointer to character without cast... im being dumb again, i can feel it

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

typedef struct node{ int *datapointer; struct node *next; } NODE;

int empty_list( NODE *L ){

  return( L == NULL ) ? 1 : 0;
}
int comparelist( int a, int b ){

  if( a == b ) return 1;
  else return 0;
}

int equal( NODE *L1, NODE *L2, int( *p_cmp_f )() ){

  if( empty_list( L1 ) && empty_list( L2 )) return 1;
  if( empty_list( L1 ) && !empty_list( L2 )) return 0;
  if( !empty_list( L1 ) && empty_list( L2 )) return 0;

  return((*p_cmp_f)(DATA(L1),DATA(L2)) && equal(NEXT(L1), NEXT(L2), p_cmp_f));
}
int main( int argc, char *argv[] ){

  NODE *L1, *L2;
  NODE *temp ;
  int n, i, num;

  printf( "\n How many numbers do you want to enter for the first list? " );
  scanf( "%d", &n );

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

    temp = ( NODE * ) malloc( sizeof( NODE ) );

    printf( "Please enter a number: " );
    scanf( "%d", &num );

//warning here makes pointer to int without cast
    temp -> datapointer = num;
    temp -> next = L1;
    L1 = temp ;
  }
  printf( "\n How many numbers do you want to enter for the second list? " );
  scanf( "%d", &n );

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

    temp = ( NODE * ) malloc( sizeof( NODE ) );

    printf( "Please enter a number: " );
    scanf( "%d", &num );

//same error as above
    temp -> datapointer = num;
    temp -> next = L2;
    L2 = temp;
  }

  printf( "\n\n" );
  if( equal( L1, L2, comparelist ) == 1 )
    printf( "The lists are equal." );

  if( equal( L1, L2, comparelist ) == 0 )
    printf( "The lists are not equal. " );

  return( 0 );
}

typedef struct node{ int *datapointer; struct node *next; } NODE; You probably want int *datapointer to be int data , because of how you are using it here (you are trying to assign an integer to a pointer): temp -> datapointer = num; Otherwise you'd have to allocate space for the integer you want to assign, assign the integer to that allocated space, and store the pointer to the space in datapointer. Seems overly complicated just for an integer!

Yet another defects (obvious):
1. The comparelist function does not compare lists, it compare two integers (now). Moreover, the 3rd parameter of equal semantically defines "compare nodes" function! The natural 3rd parameter prototype is int(*p_cmp_f)(const int*,const int*) or even int(*p_cmp_f)(const NODE*,const NODE*).
2. Extremely ineffective function equal implementation. For example, two last ifs are exactly the same as:

if (empty_list(L1) || empty_list(L2)) return 0;

No need in recursion: the simplest loop over two list is more clear and effective.

Apropos, no need in this clumsy (*p_cmp_f)(...) call. Do it in a simple and clear manner:

p_cmp_f(...)

thanks u guys, i messed around with it for a while and got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.