#include <iostream>
using namespace std;
template <class T>
class Stack;
template <class T>
class SNode{
T _data;
SNode<T>* _next;
SNode(T data, SNode<T>* next){
_data = data;
_next = next;
}
friend class Stack<T>;
};
template <class T>
class Stack{
SNode<T>* _top;
public:
Stack();
void push(T data);
T pop();
bool isEmpty()const;
virtual ~Stack();
};
template <class T>
Stack<T>::Stack(){
_top = (SNode<T>*)0;
}
template <class T>
void Stack<T>::push(T data){
SNode<T>* temp = new SNode<T>(data,_top);
_top = temp;
}
template <class T>
T Stack<T>::pop(){
T val = _top->_data;
SNode<T>* toDel = _top;
_top = _top->_next;
delete toDel;
return val;
}
template <class T>
bool Stack<T>::isEmpty()const{
return !_top;
}
template <class T>
Stack<T>::~Stack(){
SNode<T>* toDel;
while(_top){
toDel = _top;
_top = _top->_next;
delete toDel;
}
}
int main(){
Stack<int> S;
int i;
for(i=100;i<2000;i+=10){
S.push(i);
}
while(!S.isEmpty()){
cout<<S.pop()<<" ";
}
cout<<endl<<"*******************************************"<<endl;
for(i=100;i<2000;i+=10){
S.push(i);
}
for(i=0;i<10;i++){
cout<<S.pop()<<" ";
}
cout<<endl<<"*******************************************"<<endl;
return 0;
}
No comments:
Post a Comment