Possible Duplicate:
Reverse a singly linked list
reverse a linked list?
I have been trying to figure out how to displays the contents of a linked list of numbers in reverse order. I tried changing nodes around and i cant figure it out. its a singly linked list. I thought of implementing a stack. is there a simpler way? heres my code. i need the function to be in the header file, and it will be implemented in the main.cpp. ive been trying for hours today and yesterday and this is really a last resort. i have pages of notes of failed algorithms. as well as pages of google searches but nothing seems to work. if it makes a difference im using Microsoft Visual Studio 2010 (not my first choice). the code i presented does not have what ive been trying as it all failed horribly and usually produced errors.
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
#include <iostream>
#include <string>
#include <stack>
#include "Node.h"
using namespace std;
using namespace bruno;
namespace bruno
{
template< typename T>
class LinkedList
{
private: Node<T> * head;
Node<T> * tail;
int numberOfNodes;
public: LinkedList();
~LinkedList();
int length();
Node<T> * getHead() { return head; };
Node<T> * getTail() { return tail; };
void insertInFront(T d);
void insertInBack(T d);
static void listContents( Node<T> * head);
static T sum( Node<T> * head); // assume head is not NULL at call
static void increment( Node<T> * head, T val);
static void reverseListContents( Node<T> * head, Node<T> * tail, int n );
};
template< typename T >
LinkedList<T>::LinkedList()
{
head = tail = NULL;
numberOfNodes = 0;
}
template< typename T >
LinkedList<T>::~LinkedList()
{
}
template< typename T >
int LinkedList<T>::length()
{
return numberOfNodes;
}
template< typename T >
void LinkedList<T>::insertInFront(T d)
{
Node<T> * p = new Node<T>;
p->data = d;
p->next = head;
head = p;
if ( tail == NULL )
tail = p;
numberOfNodes++;
}
template< typename T >
void LinkedList<T>::insertInBack(T d)
{
if ( tail == NULL )
insertInFront(d);
else {
tail->next = new Node<T>;
tail = tail->next;
tail -> data = d;
tail -> next = NULL;
}
numberOfNodes++;
}
template< typename T >
void LinkedList<T>::listContents(Node<T> * head)
{
if (head == NULL) return;
else
{ cout << head->data << " -> ";
listContents( head -> next );
}
}
template< typename T >
T LinkedList<T>::sum(Node<T> * head)
{
if ( (head->next) == NULL ) return head->data;
else
return ( head->data + sum( head->next ) );
}
template< typename T >
void LinkedList<T>::increment(Node<T> * head, T val)
{
if ( head == NULL ) return;
else
{
head->data += val; // add val to current data value
increment( head->next, val );
}
}
template< typename T >
void LinkedList<T>::reverseListContents (Node<T> * head, Node<T> * tail, int n)
{
//clueless!
}
}
#endif
You can do it this way:
1) Find the last node and print it.
2) Traverse the linked list to find the node that has the node you last printed as its next node, and print it.
3) Go to step 2 until there are no more nodes to print.