Thursday, January 20, 2011

Check equality for linked list

Input:

p = 1 > 2 > 3 > 4 > NULL;
q = 1 > 2 > 3 > 4 > NULL;

Output: TRUE

#define TRUE 1
#define FALSE 0

int check(NODE *p, NODE *q) {

    if( p == NULL && q == NULL)
        return TRUE;
        
    while( p != NULL ) {
        
        if( q == NULL )
            return FALSE;
            
        if( p->info != q->info )
            return FALSE;            
        
        p = p->next;
        q = q->next;
    }
}

No comments :

Post a Comment