User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,859 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Oct 18th, 2007
Views: 3,270
This will works Recusivly and easily
Last edited : Oct 18th, 2007.
c Syntax | 4 stars
  1. void rev(node *h,node *myparentaddr)
  2. {
  3. if(h->next!=NULL)
  4. {
  5. rev(h->next,h);
  6. tail=h;
  7. }
  8. else
  9. head=h;
  10. h->next=myparentaddr;
  11. }
  12.  
  13. call it in main by rev(head,NULL);
Comments (Newest First)
jessel | Newbie Poster | Feb 26th, 2008
hello do you have a program in c that deals in linked list?
please help me
farwa | Newbie Poster | Feb 7th, 2008
hello every one i m new to c language can any one help me in typing a marksheet in c.
Duoas | Posting Virtuoso | Nov 9th, 2007
Nice, but it fails on three counts:
1. It doesn't return the new head of the list.
2. It fails if h is NULL.
3. It fails on really big lists (stack overflow).

Here's an iterative solution you might enjoy.
  1. /*
  2.   Reverse a singly linked list without recursion.
  3.   The argument may be NULL.
  4.   Returns the new head of the list.
  5.   Runs in linear time and with constant memory usage.
  6.  */
  7. node *rev( node *head ) {
  8. node *next;
  9. node *curr = head;
  10. node *prev = NULL;
  11.  
  12. while (curr != NULL) {
  13. next = curr->next;
  14. curr->next = prev;
  15.  
  16. prev = curr;
  17. curr = next;
  18. }
  19.  
  20. return prev;
  21. }
Call it thus:
head = rev( head );

Enjoy!
jai_steave | Newbie Poster | Oct 18th, 2007
it works recursively
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:56 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC