how to reverse link list using recurtion

Reply

Join Date: Jun 2005
Posts: 5
Reputation: happyshub is an unknown quantity at this point 
Solved Threads: 0
happyshub's Avatar
happyshub happyshub is offline Offline
Newbie Poster

how to reverse link list using recurtion

 
0
  #1
Jun 8th, 2005
please can someone give me code how to reverse single link list using recurtion

thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: how to reverse link list using recurtion

 
0
  #2
Jun 8th, 2005
Originally Posted by happyshub
please can someone give me code how to reverse single link list using recurtion
Show an attempt.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 5
Reputation: happyshub is an unknown quantity at this point 
Solved Threads: 0
happyshub's Avatar
happyshub happyshub is offline Offline
Newbie Poster

Re: how to reverse link list using recurtion

 
0
  #3
Jun 8th, 2005
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 :-)

  1. struct node *p,*q,*r;
  2. r=null;
  3. p=head;
  4. while(p)
  5. {
  6. q=p->next;
  7. p->next=r;
  8. r=p;
  9. p=q;
  10. }
  11. head=p;



With recurtion :
  1. list *tail;
  2.  
  3. list *reverse (list *head)
  4. {
  5. if (head->next == NULL)
  6. {
  7. tail = head;
  8. return;
  9. }else{
  10. reverse (head->next);
  11. head->next->next = head;
  12. head->next = NULL;
  13. }
  14. }
<< moderator edit: added [code][/code] tags >>



In general can we reverse link list using REFERERCNCE to pointer as function parameter ....
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: how to reverse link list using recurtion

 
0
  #4
Jun 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 5
Reputation: happyshub is an unknown quantity at this point 
Solved Threads: 0
happyshub's Avatar
happyshub happyshub is offline Offline
Newbie Poster

Re: how to reverse link list using recurtion

 
0
  #5
Jun 8th, 2005
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:
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: how to reverse link list using recurtion

 
0
  #6
Jun 8th, 2005
>> 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:
  1. list *reverse (list *head)
  2. {
  3. if (head->next == NULL)
  4. {
  5. tail = head;
  6. return;
  7. }else{
  8. reverse (head->next);
  9. head->next->next = head;
  10. head->next = NULL;
  11. }
  12. }
Here's my modification using a local temporary:
  1. link *reverse_list(link *head)
  2. {
  3. link *temp;
  4.  
  5. if (head->next == 0) temp = head;
  6. else {
  7. temp = reverse_list(head->next);
  8. head->next->next = head;
  9. head->next = 0;
  10. }
  11.  
  12. return temp;
  13. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 5
Reputation: happyshub is an unknown quantity at this point 
Solved Threads: 0
happyshub's Avatar
happyshub happyshub is offline Offline
Newbie Poster

Re: how to reverse link list using recurtion

 
0
  #7
Jun 8th, 2005
Ohhh thanks .....Its finally working ..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC