| | |
how to reverse link list using recurtion
![]() |
•
•
•
•
Originally Posted by happyshub
please can someone give me code how to reverse single link list using recurtion
"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
•
•
•
•
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 :-)
C Syntax (Toggle Plain Text)
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 :
C Syntax (Toggle Plain Text)
list *tail; list *reverse (list *head) { if (head->next == NULL) { tail = head; return; }else{ reverse (head->next); head->next->next = head; head->next = NULL; } }
In general can we reverse link list using REFERERCNCE to pointer as function parameter ....
•
•
•
•
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.
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:
Here's my modification using a local temporary:
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.
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:
C Syntax (Toggle Plain Text)
list *reverse (list *head) { if (head->next == NULL) { tail = head; return; }else{ reverse (head->next); head->next->next = head; head->next = NULL; } }
C Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Link List Insert problems (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
- circular link list (C)
Other Threads in the C Forum
- Previous Thread: A simple query
- Next Thread: Problems with changing values returned by RegQueryValueEx
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






