我的模板栈

 1 template<typename T>
 2 struct Node
 3 {
 4     T data;
 5     Node<T>* next;
 6 };
 7 
 8 template<typename T>
 9 class stack
10 {
11     Node<T>* top;
12 public:
13     stack():top(NULL){}
14     void push(T n);
15     T pop();
16     bool stackEmpty();
17     T getTop();
18     ~stack(){}
19     void print();
20 };
21 
22 template<typename T>
23 void stack<T>::push(T n)
24 {
25     Node<T>* r=new Node<T>;
26     r->data=n;
27     r->next=top;
28     top=r;
29 }
30 
31 template<typename T>
32 T stack<T>::pop()
33 {
34     Node<T> *ptr = top;  
35     top = top->next;
36     T t=ptr->data;
37     delete ptr;
38     return t;
39 }
40 
41 
42 template<typename T>
43 void stack<T>::print()
44 {
45     for(Node<T>* p=top;p;p=p->next)
46         cout<<p->data<<"  ";
47 }
48 
49 template<typename T>
50 T stack<T>::getTop()
51 {
52     return top->data;
53 }
54 
55 template<typename T>
56 bool stack<T>::stackEmpty()
57 {
58     if(top)return false;
59     else return true;
60 }

 

posted on 2016-05-16 19:31  Kooing  阅读(115)  评论(0编辑  收藏  举报

导航