reverse linked list

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

reverse linked list

 
0
  #1
Jan 4th, 2008
hello
i want to build a function which receives a pointer to a head of linked list
reverses the order of the list and returns a pointer to the new head.

my list is defined like this :

  1. typedef struct item {
  2.  
  3. int key ;
  4.  
  5. struct item *next ;
  6.  
  7. } item ;

my fucction is :

  1. void swapitem ( item *a ) {
  2.  
  3. (a -> next) -> next = a ;
  4.  
  5. a -> next = NULL ;
  6.  
  7. } // end swapitem
  8.  
  9.  
  10. item** revList ( item *head ) {
  11.  
  12. item **p ;
  13.  
  14. if ( !head -> next ) {
  15.  
  16. **p = *head ;
  17.  
  18. return p ;
  19.  
  20. }
  21.  
  22. else {
  23.  
  24. revList (head->next) ;
  25.  
  26. swapitem (head) ;
  27.  
  28. return p ;
  29.  
  30. }
  31.  
  32. } // end revList

at first when i print the list it is ok
when i try to print the list after the function it shows nothing
please advice.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: reverse linked list

 
0
  #2
Jan 4th, 2008
When working with linked data structures, the best thing you can do is take out a piece of paper and draw the logic. Don't draw what you think is happening, draw exactly what your code does, and you'll find the bugs.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: reverse linked list

 
0
  #3
Jan 4th, 2008
I always take it a step farther and just tell people to forget the code. Get out the paper and crayons first, draw what must happen, then write the code to do exactly that.

Disentangling new programmers from bad code is more difficult and confusing than just going through the steps of writing the code correctly to begin with...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: reverse linked list

 
0
  #4
Jan 4th, 2008
>Disentangling new programmers from bad code is more
>difficult and confusing than just going through the steps
>of writing the code correctly to begin with...
Writing code correctly is all well and good, but learning to debug broken code is just as important (if not more so).
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: reverse linked list

 
1
  #5
Jan 4th, 2008
Agreed, but when dealing with new programmers I have learned by experience (teaching and tutoring) that debugging works best once a firm concept of what should be happing is established. Anyway...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: reverse linked list

 
0
  #6
Jan 5th, 2008
ok i did it all i had to do is turn my pointer static

  1. item *revList ( item *head ) {
  2.  
  3. static item* p ;
  4.  
  5. if ( !head -> next ) {
  6.  
  7. p = head ;
  8.  
  9. return head ;
  10.  
  11. }
  12.  
  13. revList (head->next) ;
  14.  
  15. swapitem (head) ;
  16.  
  17. return p ;
  18.  
  19. } // end revList

thanks everyone for the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2203 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC