| | |
Reversing a linked list using Recursion
![]() |
•
•
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 binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





