Thread: compare backward ll ints

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Tracing _Mike's code by inspection, it looks like it should work for digit linked lists that are not equal. Perhaps you can try this instead:
    Code:
    int compare(const struct integer* lhs, const struct integer* rhs)
    {
        int result = 0;
    
        while (lhs && rhs)
        {
            if (lhs->digit < rhs->digit)
            {
                result = -1;
            }
            else if (lhs->digit > rhs->digit)
            {
                result = 1;
            }
            /* Ignore equal digits. */
    
            lhs = lhs->next;
            rhs = rhs->next;
        }
    
        if (lhs)
        {
            return 1;
        }
        else if (rhs)
        {
            return -1;
        }
        else
        {
            return result;
        }
    }
    Cool beans! this works perfectly.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by _Mike View Post
    Hmm that's weird.
    Could you please give me an example of numbers which doesn't work so I can verify if I messed something up?
    I didn't try anything specific. Take any two numbers and see the results for yourself.

  3. #18
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by itCbitC View Post
    Take any two numbers and see the results for yourself.
    I have, which is why I thought it was weird that it's not working for you
    Here are the results I get from comparing 11345, 12345 & 107483 against each other.
    (manually formatted to look better here on the forums, but otherwise identical to the original output)
    Code:
     11345 ==  11345 :  0
     11345 ==  12345 :  1
     11345 == 107483 :  1
     12345 ==  11345 : -1
     12345 ==  12345 :  0
     12345 == 107483 :  1
    107483 ==  11345 : -1
    107483 ==  12345 : -1
    107483 == 107483 :  0
    Full source for reference:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    struct integer
    {
    	int digit;
    	struct integer* next;
    };
    
    struct integer* make_int(const char* strnum)
    {
    	int i, offset = 0;
    	struct integer *ret, *current, *next;
    	if(!strnum)
    		return 0;
    	while(strnum[offset] == '0')
    		offset++;
    
    	ret = malloc(sizeof(*ret));
    	ret->digit = 0;
    	ret->next = 0;
    	next = ret;
    
    	i = strlen(strnum + offset);
    	while(i)
    	{
    		current = next;
    		current->digit = strnum[--i + offset] - '0';
    		if(i)
    			next = malloc(sizeof(*next));
    		else
    			next = 0;
    		current->next = next;
    	}
    	return ret;
    }
    
    void free_int(struct integer* num)
    {
    	struct integer* current = num;
    	while(num)
    	{
    		current = num;
    		num = num->next;
    		free(current);
    	}
    }
    
    void print_int(const struct integer* num)
    {
    	if(num)
    	{
    		print_int(num->next);
    		putchar(num->digit + '0');
    	}
    }
    
    int compare(const struct integer* l, const struct integer* r)
    {
    	/* comparison value */
    	int comp = 0;
    
    	/* test for NULL pointers */
    	if(!l && !r) return 0;
    	if(!l) return 1;
    	if(!r) return -1;
    
    	while(1)
    	{
    		/* if both numbers are of equal length,
    		 * return the comparison value */
    		if(!l && !r) return comp;
    
    		/* test the length of the numbers */
    		if(l->next && !r->next) return -1;
    		if(!l->next && r->next) return 1;
    
    		/* compare digits */
    		if(l->digit < r->digit) comp = 1;
    		else if(l->digit > r->digit) comp = -1;
    
    		/* move to next digit */
    		l = l->next;
    		r = r->next;
    	}
    }
    
    void compare_and_print(const struct integer* lhs, const struct integer* rhs)
    {
    	print_int(lhs); printf(" == "); print_int(rhs); printf(" : %d\n", compare(lhs, rhs));
    }
    
    int main()
    {
    
    	struct integer* num1 = make_int("11345");
    	struct integer* num2 = make_int("12345");
    	struct integer* num3 = make_int("107483");
    
    	compare_and_print(num1, num1);
    	compare_and_print(num1, num2);
    	compare_and_print(num1, num3);
    	compare_and_print(num2, num1);
    	compare_and_print(num2, num2);
    	compare_and_print(num2, num3);
    	compare_and_print(num3, num1);
    	compare_and_print(num3, num2);
    	compare_and_print(num3, num3);
    
    	free_int(num1);
    	free_int(num2);
    	free_int(num3);
    
    	return 0;
    }

  4. #19
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    haha, well when i posted this original question, it was just for one method, the compare. the actual program wanted to add the two linked lists and subtract them. the meaning for the compare method was that whne you subtract the two linked lists, you must subtract the smaller one from the bigger one

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Soulzityr
    haha, well when i posted this original question, it was just for one method, the compare.
    _Mike just posted a larger program so that itCbitC could test it since itCbitC ran into a problem when testing _Mike's example. Personally, I find _Mike's example difficult to trace. You can refer to my post #14 for an example that should be easier to trace and show to be correct (but then I am biased because I wrote it ).

    EDIT:
    To elaborate on why I find _Mike's code difficult to trace: I tend to feel uneasy when a pointer is dereferenced and I cannot easily see that it is valid and not a null pointer. The problem is that the check for null pointers in the current iteration is performed on the previous iteration (or before the loop, for the first iteration). Consequently, I had to carefully go through the first two iterations before I became convinced that it should be correct.

    In my version, on the other hand, it is easy to see that both pointers cannot be null pointers within the loop. Therefore, you do not need to worry about that, and can immediately proceed to check that the comparison logic is correct. It is only after the loop terminates that I consider the cases of whether one or both the pointers are null pointers. (Plus, you can easily tell that this must be so as the cases are a guaranteed post-condition of the loop.)
    Last edited by laserlight; 02-28-2010 at 01:24 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    laserlight:
    I agree that your code is easier to read and less error-prone than mine. I have never had any proper (classes, books, etc) programming training. I've basically just learned using the "monkey method", writing some code and prodding it with a stick until it moves. So while I might know quite a bit on the technical side of programming I have never really put much thought in to good code design.
    But it is posts like yours that makes me learn more, so thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Mayhem
    By thomas41546 in forum C Programming
    Replies: 3
    Last Post: 05-11-2008, 10:25 AM
  2. how to handle ints and strings when writing to file
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 04-23-2008, 04:44 AM
  3. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 06:12 AM
  4. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 09:42 PM
  5. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM