It might be helpful to have a little more context for this code, to answer questions such as why you don't do something like this:
#include <list>
...
std::list<int> my_list;
... put something in my_list to make a non-trivial test ...
show_list(my_list);
my_list.reverse();
show_list(my_list);
There's also a singly-linked list, std::slistslist
, if you want to avoid the double-linking of std::liststd::list
, although I've previously found std::slistslist
not as portable as std::liststd::list
. (That is, not everyone's implementation of the STL seemed to provide it.)
If it's really worth creating your own linked list, how about making a class called something like ListList
containing objects of type NodeNode
, or implement these as templates List<T>List<T>
and Node<T>Node<T>
as others have recommended. Then? Then you can make reverse()reverse()
be a member function of List<T>List<T>
so that you can write something like:
List<T> my_list;
... other code ...
my_list.reverse();
A member function returning void would be my preferred way to modify an object "in place"--the function doesn't return anything, and in this case it doesn't even have parameters to play with, so it's clear the ONLY thing it can do is to modify the original list. And notice that that's how list reversal is implemented in STL.