How do you reverse the nodes in a singly linked list?
Here is the struct and a definition of a function i was working on.
Code:
// ptr, passed from main, points to the first node in the linked list
// i think. In this function i'm trying to reverse the list and print
// the 'name' members in each node in the reversed arrangement.
...
struct Elephant {
string name;
Elephant* next;
};
...
void reverse_elephant( const Elephant* ptr )
{
const Elephant *temp[50], *curr, *first;
int x=0;
curr = ptr;
while ( ptr != 0) {
temp[x++] = ptr;
ptr = ptr -> next;
}
cout << "x= " << x << '\n';
ptr = curr;
first = ptr = temp[x];
for (int i=x-1; i>0; i--){
ptr = ptr -> next = temp[x];
cout << "Name= " << ptr -> name << '\n';
}
}
im lost. I know this is a sorry attempt but i'm out of ideas. Can anyone help?