template <class T>
class Stack;
template <class T>
class Node{
T _data;
Node<T>* _next;
Node(T data, Node<T>* next);
friend class Stack<T>;
};
template <class T>
class Stack{
Node<T>* _top;
public:
Stack(void);
virtual ~Stack(void);
void push(T data);
T pop(void);
bool isEmpty(void);
};
template <class T>
Node<T>::Node(T data, Node<T>* next){
_data = data; // T must be ok with operator =
_next = next;
}
template <class T>
Stack<T>::Stack(void){
_top = (Node<T>*)0;
}
template <class T>
Stack<T>::~Stack(void){
Node<T>* toDel;
while(_top){
toDel = _top;
_top = _top->_next;
delete toDel;
}
}
template <class T>
void Stack<T>::push(T data){
Node<T>* tempnode = new Node<T>(data, _top);
_top = tempnode;
}
template <class T>
T Stack<T>::pop(void){
T ret = _top->_data; // T needs should be ok with copying
Node<T>* toDel = _top;
_top = _top->_next;
delete toDel;
return ret;
}
template <class T>
bool Stack<T>::isEmpty(void){
return _top == (Node<T>*)0;
}
没有评论:
发表评论