what are the bugs in this code..please help me.

This is a discussion on what are the bugs in this code..please help me. within the C Programming forums, part of the General Programming Boards category; This code is intended to merge, two sorted, linked lists into a single sorted, linked list: it is supposed to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    what are the bugs in this code..please help me.

    This code is intended to merge, two sorted, linked lists into a single sorted, linked list: it is supposed to take two lists as input and destructively modify them so that at the end, they form a single, sorted, linked list in memory.
    What are bugs in the code?

    Code:
    typedef struct link {
    char *data;
    struct link *next;
    } list;
    
    
    list *merge(list *input1, list *input2) {
    list *output = NULL, *tail, *next;
    do {
    if(strcmp(input1->data, input2->data) <= 0) {
    next = input1;
    input1 = input1->next;
    } else {
    next = input2;
    input2 = input2->next;
    }
    if (output == NULL) {
    output = tail = next;
    } else {
    tail->next = next;
    tail = tail->next;
    }
    } while (input1 != NULL && input2 != NULL);
    if (input1 != NULL) tail->next = input1;
    else tail->next = input2;
    return output;
    }
    Last edited by me001; 09-23-2008 at 09:08 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Where did you get this indent style?

    How quickly can you lose it?

    Put a print statement just at the inside end of your while loop, and you should see the problem.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    thanks

    i am not a C person..I am working as SQL DBA.


    modified indent style.. what is c indent style?.

    what compiler i need to use to compile this code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    what compiler i need to use to compile this code?
    A C compiler, obviously. Even then, your code is incomplete as it does not include the appropriate header files.

    How did you get this code in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    i am learning C for a project...forgot c...no touch for 10 years now.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sorry if this sounds harsh, but have you considered that learning to do something actually means that you have to practice, rather than asking someone else to do it for you?

    Indent style:
    Code:
    typedef struct link {
        char *data;
        struct link *next;
    } list;
    
    
    list *merge(list *input1, list *input2) {
        list *output = NULL, *tail, *next;
        do {
    	if(strcmp(input1->data, input2->data) <= 0) {
    	    next = input1;
    	    input1 = input1->next;
    	} else {
    	    next = input2;
    	    input2 = input2->next;
    	}
    	if (output == NULL) {
    	    output = tail = next;
    	} else {
    	    tail->next = next;
    	    tail = tail->next;
    	}
        } while (input1 != NULL && input2 != NULL);
        if (input1 != NULL) tail->next = input1;
        else tail->next = input2;
        return output;
    }
    Note that you will want to avoid mixing tabs and spaces, as tabs can be different size on different systems. [The above is indented using XEmacs and C-style "stroustrup", using the M-X indent-region command].



    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    thanks...

    learned something.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Please post some C tutorials

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by me001 View Post
    Please post some C tutorials
    We're not your lackeys! Search the web yourself!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    how much you put me down...i will come back strong... GOD


    Bug 1 :
    line 8: variable 'NULL' not found
    'list *output = NULL, *tail, *next'
    aborting compile

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Bug 1 :
    line 8: variable 'NULL' not found
    'list *output = NULL, *tail, *next'
    aborting compile
    That's because the appropriate headers (<stddef.h> and also <string.h>) were not included. NULL is from <stddef.h>, though <string.h> may include it as well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    thanks...got it...

    line 1: Parse Error, expecting `SEP'
    '<stddef.h> <string.h> typedef struct link { char *data'
    aborting compile

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    It looks like you need to refresh your knowledge of the C preprocessor. You along with several other people here. It's like a rash.
    Last edited by whiteflags; 09-23-2008 at 10:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21