|
发表于 2014-11-22 18:40:29
|
显示全部楼层
- #include<iostream>
- #include <string>
- using namespace std ;
- template <class T>
- class Stack
- {
- private:
- int size ;
- //int top = 0 ;
- int top;
- T *array ;
- public :
- //Stack(int size)
- Stack(int size):top(0)
- {
- this->size = size ;
- array = new T[size] ;
- }
- ~Stack() { delete []array ; }
-
- void push(T data) { array[top++] = data ; }
-
- T pop()
- {
- top-- ;
- cout<<" we get "<<array[top]<<endl;
- return array[top];
- }
- };
- int main()
- {
- Stack<int> A(7) ;
- A.push(22) ;
- A.push(44) ;
- A.push(66) ;
-
-
- A.pop() ;
- A.pop() ;
- A.pop() ;
-
- Stack<string> B(9) ;
- B.push("string_1") ;
- B.push("string_2") ;
- B.push("string_3") ;
-
- B.pop();
- B.pop();
- B.pop();
-
- Stack<char> C(10) ;
- C.push('a') ;
- C.push('b') ;
- C.push('c') ;
-
- C.pop() ;
- C.pop() ;
- C.pop() ;
- }
复制代码 |
|