DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   how to reverse link list using recurtion (http://www.daniweb.com/forums/thread25269.html)

happyshub Jun 8th, 2005 4:21 am
how to reverse link list using recurtion
 
please can someone give me code how to reverse single link list using recurtion

thanks

Dave Sinkula Jun 8th, 2005 11:30 am
Re: how to reverse link list using recurtion
 
Quote:

Originally Posted by happyshub
please can someone give me code how to reverse single link list using recurtion

Show an attempt.

happyshub Jun 8th, 2005 12:26 pm
Re: how to reverse link list using recurtion
 
Quote:

Originally Posted by Dave Sinkula
Show an attempt.


Ohhh I am new to the forum and I was not knowing the rules :mrgreen: :p That I have to show attempt ...

Here is what I was trying to do ! :evil: :evil:

Without Recurtion :-)

struct node *p,*q,*r;
r=null;
p=head;
while(p)
{
    q=p->next;
    p->next=r;
    r=p;
    p=q;
}
head=p;



With recurtion :
   list *tail;  
 
list *reverse (list *head) 

  if (head->next == NULL) 
  { 
    tail = head; 
    return; 
  }else{ 
    reverse (head->next); 
    head->next->next = head; 
    head->next = NULL; 
  } 
}
<< moderator edit: added [code][/code] tags >>



In general can we reverse link list using REFERERCNCE to pointer as function parameter .... :idea:

Dogtree Jun 8th, 2005 12:58 pm
Re: how to reverse link list using recurtion
 
You're on the right track. Try incorporating a temporary link into your function rather than trying to work just with head and its next links.

happyshub Jun 8th, 2005 1:41 pm
Re: how to reverse link list using recurtion
 
Quote:

Originally Posted by Dogtree
You're on the right track. Try incorporating a temporary link into your function rather than trying to work just with head and its next links.

Temperary variable to be passed in recursive function :evil: ??

temparary variable are temparary to scope of function and in recursive function we use global variable sort of things :rolleyes:

In general we can use static pointer but then toomit does not workk


CAN ANYONE HELP ME ITS URGENT :cry: :cry:

Dogtree Jun 8th, 2005 1:56 pm
Re: how to reverse link list using recurtion
 
>> Temperary variable to be passed in recursive function
No.

>> temparary variable are temparary to scope of function
Yea.

>> and in recursive function we use global variable sort of things
What are you babbling about? It's possible and perfectly reasonable to use a non-static local variable in a recursive function. This function doesn't need a static variable or a global variable. Here's your current function:
list *reverse (list *head)
{
  if (head->next == NULL)
  {
    tail = head;
    return;
  }else{
    reverse (head->next);
    head->next->next = head;
    head->next = NULL;
  }
}
Here's my modification using a local temporary:
link *reverse_list(link *head)
{
  link *temp;

  if (head->next == 0) temp = head;
  else {
    temp = reverse_list(head->next);
    head->next->next = head;
    head->next = 0;
  }

  return temp;
}
The algorithm is identical, but my function is completely self-contained and works properly whereas yours would have unnecessary coupling with that global variable if you kept following the same line of thinking.

happyshub Jun 8th, 2005 11:56 pm
Re: how to reverse link list using recurtion
 
Ohhh thanks .....Its finally working ..


All times are GMT -4. The time now is 9:48 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC