Thread: linked list recursive help

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    9

    linked list recursive help

    for example :
    link a:5->4->3->2->1->NULL
    link b: NULL
    Code:
    typedef struct node* link;
    struct node { int item; link next; };
    ....
    link no_scan(int n, link a, link b)
    {
        int n1;
        link xs1, xs2, c = NULL;
        if(n == 0)
        {
            b = a;
            return NULL;
        }
    
        if(n == 1)
        {
            b = a->next;      //here will be th problem
            a->next = NULL;
            return a;
        }
        n1 = n/2;
        xs1 = no_scan(n1,a,b);
        xs2 = no_scan((n-n1),b,c);   ///when i hit here, error, cos i pass(1,NULL,NULL);
        b = c;
        return merge(xs1,xs2);
    }
    i wanna make sure b has changed in the call no_scan(n1,a,b);
    can anyone one help me, thanks a lot
    Last edited by dreamkk; 10-09-2003 at 11:05 PM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You can't change the inputs to a function in this way. For example:
    Code:
    void f(int n)
    {
      n = 49;
    }
    
    void g (int * pN)
    {
      *pN = 345;
    }
    
    int main (void)
    {
      int n = 20;
    
      f(n);
      printf("after f(n), n is now: %d\n", n);
    
      g(&n);
      printf("after g(&n), n is now: %d\n", n);
    
      return 0;
    }
    
    /*
    output:
    after f(n), n is now: 20
    after g(&n), n is now: 345
    */
    I think that may be your problem...
    DavT
    -----------------------------------------------

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Variable b is a pointer, if you want to change a pointer, you need to pass the address of the pointer. The function header would then look like this:

    link no_scan(int n, link a, link *b)

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    9
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM