Thread: Linked list and recursion

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Question Linked list and recursion

    Hello programmers ,


    I have to develope a recursive function in C.


    Given a linked list and node:

    First --> [7][ ] --> [23] [ ] --> [9] [ ] --> [43] [ ] --> [51] [•]

    Code:
    Typdef  struct node {
                Unsigned data;
       	Struct  node *next;
    } node
    I have now to write a recursive function Minimum() wich returns smallest data element. (so smallest data element from node).

    Please can anybody help me???

    Thanx in advance from Holland

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's your attempt at it? Let's see your code or pseudo code of how you think it should work.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    I think I have it, but not recursive
    I must have it recursive


    Code:
    int MinList (NodePtr theList) {
      int min = theList -> data;		//set minimum to first data
      NodePtr currNodePtr = theList->next; //set pointer to next node
      
      while(currNodePtr != NULL) {
        if(currNodePtr->data < min) {
          min = currNodePtr-> data;
        } //if current data is less than current min
        
        currNodePtr = currNodePtr -> next;	//move to next node
      }
      return min;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To make a recursive method, you first need a way to stop recursion. Check the passed argument's 'next' pointer to see if it's null. If it is, stop with this comparison. If it's not, pass it along.

    Next, you need to do a comparison with this node's value compared to what is returned by the recursive call. Return which ever of the two is greater. That should get you started. (And finished.)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Hi Quzah or other programmers,

    Can you help me more pls.
    I now have the following:
    Code:
    unsigned Miniumum(node **n,int number) {
           if (*n  != NULL)  
                  return number;
           else return Minimum(&(*n)->next,number)
    }
    But I think it is not good.
    My function prototype should be:

    unsigned Minimum(node **n);

    Help me pls more,

    thanx in advance from Holland

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    ignore that function - it's indeed bad.
    Think of the following
    if current node is NULL stop
    for each iteration store the smaller number and the current number.
    if smaller> current .. bla bla bla
    pass next node to function.

    of course this should be better if done inside a cicle. Function calls are much less efficient than a single cicle.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Hi xErath or other people,

    xErath thanx for message.
    When I understand you good, you mean:
    Code:
    unsigned Min(node *n) {
       unsigned min;
    
       if (n == NULL)
           return NULL;
       else if (min < n->data) {
           min = n->data;
           return  Min(&n->next);
       }
    }
    Help me if I am wrong. I am thinking of initialising min????

    Thanx in advance everybody

    Holland

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might try using static.

    Here's how I would do it:
    Code:
    unsigned min(node *n) {
        unsigned other;
    
        if(!n->next) return n->data;
    
        other = min(n->next);
    
        if(other < n->data) return other;
        else return n->data;
    }
    I think that might work.

    [edit]
    The above code assumes that n != NULL.
    [/edit]
    Last edited by dwks; 07-05-2005 at 12:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thanx for your code dwks

    Is this a right sollution?

    Thanx for answer

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwks thinks so.

    Ask quzah.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just try it and find out?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List by recursion
    By imliklikhk in forum C Programming
    Replies: 3
    Last Post: 02-13-2005, 10:27 AM
  2. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. linked list find
    By confuted in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2003, 03:30 PM
  5. Recursion using linked list
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 03-31-2002, 10:15 PM