please can someone give me code how to reverse single link list using recurtion
thanks
please can someone give me code how to reverse single link list using recurtion
Show an attempt.
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:
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.
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:
>> 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.
Ohhh thanks .....Its finally working ..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.