代码:
#include<iostream>
using namespace std;
template <typename T>
struct node{
T val;
node *next;
node(){
next=NULL;
}
node(T x){
val=x;
next=NULL;
}
};
template <typename T>
class Stack{
private :
node<T> *l,*r;
int len;
public :
Stack(){
node<T> *t=new node<T>();
l=r=t;
len=0;
}
~Stack(){
cout<<"析构"<<endl;
}
bool empty(){
if(len==0) return true;
return false;
}
int size(){
return len;
}
T& top(){
if(len) return r->val;
else cout<<"NULL"<<endl;
}
void push(const T &a){
node<T> *tmp=new node<T>(a);
tmp->next=r;
r=tmp;
tmp=NULL;
len++;
}
void pop(){
if(len){
len--;
r=r->next;
}
}
};
int main(){
Stack<int> S;
S.push(1);
S.push(2);
S.push(3);
while(!S.empty()){
cout<<S.top()<<endl;
S.pop();
}
return 0;
}