| | |
Reversing a linked list using Recursion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi guys,
I was wondering if I could get some help on the topic of recurrsion. A week ago i was told to create a application using nodes. this week I need to convert the reverse_list fuction I made below into using recursion. any help would be great thanks.
not using recurrsion and works!
trying to use recurrsion and does not work
I was wondering if I could get some help on the topic of recurrsion. A week ago i was told to create a application using nodes. this week I need to convert the reverse_list fuction I made below into using recursion. any help would be great thanks.
not using recurrsion and works!
C++ Syntax (Toggle Plain Text)
void reverse_list (node*& head_ptr) { node* temp_ptr = head_ptr->link(); // holding 2nd node reference. node* iter_ptr = temp_ptr->link(); // hold the reference of 3rd Node OR NULL. head_ptr->set_link(0); // setting the first Node next = 0 while (temp_ptr==NULL) // looping through 2nd node to the end. { iter_ptr = temp_ptr->link(); // saving the 3rd Node. temp_ptr->set_link(head_ptr); // reversing head_ptr = temp_ptr; // incrementing head. temp_ptr = iter_ptr; // increment } }
C++ Syntax (Toggle Plain Text)
void reverse_list (node*& head_ptr) { node* current_ptr = head_ptr->link(); //holding 2nd node while (current_ptr->link() != NULL) //checking if at end of list { current_ptr->set_link(head_ptr); //reversing head_ptr = current_ptr; //incrementing reverse_list(head_ptr); //calling function again } }
•
•
Join Date: May 2008
Posts: 5
Reputation:
Solved Threads: 0
I was able to figure out another way to reverse the linked list using recursion.
C++ Syntax (Toggle Plain Text)
node* reverse_list (node* head_ptr) { node* current = NULL; if (head_ptr->link() != NULL) { current = reverse_list(head_ptr->link()); (head_ptr->link())->set_link(head_ptr); head_ptr->set_link(NULL); } else { current = head_ptr; } return current; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Any difference between C++ regex and PHP?
- Next Thread: ScrollBar on Form
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





