Thread: recursive list functions

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    15

    recursive list functions

    Hi guys,
    Can anyone see where I'm going wrong with this, I'm hopeless with C so considering I'm dong a module on it i'm struggling.

    Code:
     
    boolean wordLess(wordADT word1, wordADT word2)
    {
     if (isNull(word1)==FALSE)
     {     
            if(isNULL(word2)==FALSE)
            {      
                   if(word1->data)<(word2->data)) \*if word1 is              
                                                                                before2*\     
                   {
                         return TRUE;
                   }
                   else if(word1->data)==(word2->data) \*if if first letter is
                                                      the same, check the next letter*\
                   {
                         return wordLess(word->next,word2->next);
                   }
              else
              {    
                    return FALSE;
              }
         else
         {  
              return FALSE;
         }
     }
    }
    
    
    
    
    boolean wordEqual(wordADT word1, wordADT word2)
    
     if (isNull(word1)==FALSE)
     
      if(isNULL(word2)==FALSE)
      
       if(word1->data)==(word2->data))
       
       return TRUE;
       
       else
        
         return wordEqual(word->next,word2->next);
        
       else
        
          return FALSE;
        
      
     else
      
        return FALSE;
    I've not compiled them but I've got a feeling theyre wrong.
    Cheers guys */
    Last edited by sam.woolner; 02-26-2005 at 01:36 PM.

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    Also what does it mean by use tags properly it wouldnt let me post if i put brackets in the code????

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    uh... what the problem with code tags?
    its simple
    '['code']'
    blah
    '['/code']'

    (without the 's)

    and note there is an edit option - no need to create new posts.
    plz reformat your code
    signature under construction

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    better?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your post completely lacks any
    - code tags
    - indentation
    - curly braces

    Please use [code][/code]Tags
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    I don't understand what you mean.
    Im just asking for you to look at this first draft of two functions i have made because I am not very confident with C.

    If you can help me then tell me if you see anything wrong if you gonna lord that fact you know C over me then don't reply.

    ........ing hell you ever seen The Office its like talking to their IT supp guy

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    well > sould be < in the comparission in word less


    your functions will always return false.
    in case your about to compare the last element, then the next compare would match against NULL - thus returning false
    since you call compare again upon reaching the end of your list
    signature under construction

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    do you mean that it will go through the list until it hits the last item, but then when it calls wordLess again....
    Code:
    {
     if (isNull(word1)==FALSE)
     {
      if(isNULL(word2)==FALSE)
    that bit will be true???

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    how can i stop that??

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    oh - i talked nonsense - forget my previous post
    the function returns true if there is just ONE match.

    well what do you want it to do?
    signature under construction

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    right, I think wordLess is working now.
    Do you think it will work? I just need to do the comments for it.
    Last edited by sam.woolner; 02-26-2005 at 01:35 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well wordLess is looking about right now (logically), all you need do now is make it compile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    I'm still thinking that in the case that the words are the same it will keep recursively calling wordLess until it drops of the end of the word thus causing an error....
    Can I write this (recursive) function more effectively using headList and tailList?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sam.woolner
    I'm still thinking that in the case that the words are the same it will keep recursively calling wordLess until it drops of the end of the word thus causing an error....
    Can I write this (recursive) function more effectively using headList and tailList?
    Let's clean up your code and take a look at it, shall we?
    Code:
    boolean wordLess(wordADT word1, wordADT word2)
    {
        if( isNull( word1 ) == FALSE && isNull( word2 ) == FALSE )
        {
            if( word1->data < word2->data )
            {
                return TRUE;
            }
            else
            if( word1->data == word2->data )
            {
                return wordLess( word1->next, word2->next );
            }
        }
        return FALSE;
    }
    This is what you were doing, much cleaned up. What happens if one word is shorter than the other one, yet comes before it? You don't cover that case. Let's say, these words:

    smile
    smiles

    You would get all the way to the 'e', and your first word would run out. Thus, the check for 'isNull( word1 )' would return TRUE, making your whole check default to 'FALSE'. Meaning, in your code, "smile" would not come before "smiles". Which would be incorrect.

    I don't see the point though, in having a head / tale. The method you're using would work fine, provided you correctly handle all situations, such as the one I've described.

    Quzah.
    Last edited by quzah; 02-27-2005 at 07:04 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM