Reversing singly linked list?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

Reversing singly linked list?

 
0
  #1
Sep 9th, 2008
could someone please show me how i would be able to reverse the printout of a linked list?, i have gotten it to print out in the right order but not sure how to make to printout in reverse.

here is my current code,

  1. void print(node*& head)
  2. {
  3. node* temp;
  4.  
  5. temp = head;
  6.  
  7. while (temp != NULL)
  8. {
  9. cout << temp->base;
  10. temp = temp->next;
  11. }
  12. }

at the moment the print function prints out whats in the linked list in the correct order, i want it to do it in reverse, please help
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,875
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Reversing singly linked list?

 
0
  #2
Sep 9th, 2008
Your nodes don't have a link 'previous' (singly-linked), so as far as I can see, you'll have to store all the data in some sort of container (a vector for example, or another linked list ) and print that out in reverse order.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 14
Reputation: miepmuts is an unknown quantity at this point 
Solved Threads: 2
miepmuts miepmuts is offline Offline
Newbie Poster

Re: Reversing singly linked list?

 
2
  #3
Sep 9th, 2008
To reverse the printout of the linked list you can try something like the following:

  1. void print(node*& head)
  2. {
  3. node * temp = head;
  4. if (temp->next != null)
  5. {
  6. print(temp->next);
  7. }
  8.  
  9. cout << temp->base;
  10. }

This way you first recursively go to the last element of the linked list, and then start printing from back to the first element.

This is just theoretical though. I don't know (and don't have the tools present) if it will work in practice.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,875
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Reversing singly linked list?

 
0
  #4
Sep 9th, 2008
It will. You're absolutely right, I never thought about recursion... silly me
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

Re: Reversing singly linked list?

 
0
  #5
Sep 9th, 2008
Thanks for the replys, I tried miepmuts method and it works great, didnt think of doing it that way lol

+rep
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC