LInked List and Stack
Hey guys, I have alot of problems implementing the Stack as a Linked List.
I cannot seem to overload the ostream operator correctly.It says that the Node was not declared and I made the ostream Operator private.Please help
This is My Stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#ifndef STACK_H
#define STACK_H
//#include "Queue.h"
#include "MyException.h"
#include <iostream>
using namespace std;
template<class T>
class Stack;
template<class T>
ostream& operator<<(ostream&,Stack<T>&);
template<class T>
class Stack
{
public:
friend ostream& operator<< <T>(ostream&,Stack<T>&);
Stack();
Stack(const Stack<T>& other);
Stack<T>& operator=(const Stack<T>& other);
~Stack();
void push(const T& el);
T pop();
T peek();
bool isEmpty();
private:
class Node
{
public:
Node(const T& data, Node* n = 0)
{
element = data;
next = n;
}
T element;
Node* next;
};
Node* top;
};
#include "Stack.cpp"
#endif
|
This is my ostream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
template<class T>
ostream& operator<< (ostream& os,Stack<T>& el){
Node *temp = top;
cout << "[";
while(temp != NULL){
os << temp->el << ",";
temp = temp->next ;
}
cout << "]";
//delete temp;
return os;
}
|
Thanks.
Topic archived. No new replies allowed.