I am very new to C++ template, and learning it through small programming.
I'm definitely not understanding what is wrong -- at all.
Here is the code.
Reverse a linked List using a stack
using namespace std;
template<typename T>
void Reverse( node<T> *front);
template<typename T>
void Display( node<T> *front);
template<typename T>
class node
{
public:
T data;
node<T> *next;
node(){ next = NULL; }
node(const T& item, node<T> *nextnode = NULL )
{
data = item;
next = nextnode;
}
};
int main()
{
node<int> *front = NULL;
int size, no;
cout << "Enter the size of list ";
cin >> size;
for (int i = 1; i < size; i++)
{
cout << "Enter the " << i << " element " << endl;
cin >> no;
front = new node<int> ( no , front);
}
Reverse(front);
Display(front);
_getch();
return 0;
}
template<typename T>
void Reverse(node<T> *front)
{
node<T> *temp = front;
stack<T> s;
while (temp != NULL)
{
s.push(temp); // Pushing the address into stack
temp = temp->next;
}
temp = s.top();
front = temp;
s.pop();
while (!s.empty())
{
temp->next = s.top(); // Popping the address from stack.
s.pop();
temp = temp->next;
}
temp->next = NULL;
}
template<typename T>
void Display( node<T> *front)
{
node<T> *temp = front;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
I am getting following both errors ( errors 1 & 2 ) for both functions ( i.e. Reverse() and Display() )
Error 1 ) error C2182: 'Reverse' : illegal use of type 'void'
Error 2) error C2998: 'int Reverse' : cannot be a template definition
Error 3) error C1903: unable to recover from previous error(s); stopping compilation.
I've tried every single thing I can think of. But could not succeed.