| | |
Reversing singly linked list?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 0
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,
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
here is my current code,
C++ Syntax (Toggle Plain Text)
void print(node*& head) { node* temp; temp = head; while (temp != NULL) { cout << temp->base; temp = temp->next; } }
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
•
•
Join Date: Aug 2008
Posts: 14
Reputation:
Solved Threads: 2
To reverse the printout of the linked list you can try something like the following:
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.
C++ Syntax (Toggle Plain Text)
void print(node*& head) { node * temp = head; if (temp->next != null) { print(temp->next); } cout << temp->base; }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: simplest euler method...HELP
- Next Thread: Help Me with this problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







) and print that out in reverse order.