1 #include <iostream> 2 3 using namespace std; 4 5 template <class Node> 6 class StackNode 7 { 8 public: 9 StackNode(); 10 ~StackNode(); 11 Node node; 12 StackNode *next; 13 }; 14 15 template<class Node> 16 StackNode<Node>::StackNode() 17 { 18 19 } 20 21 template<class Node> 22 StackNode<Node>::~StackNode() 23 { 24 cout<<"destory"<<endl; 25 } 26 27 template<class T> 28 class Stack 29 { 30 public: 31 Stack(); 32 Stack(const Stack& stack); // 拷贝构造函数 33 ~Stack(); 34 void push(const T& item); //入栈 35 StackNode<T> *popup(); //出栈 36 bool isEmpty(); //判空 37 int getLength(); //获取栈长度 38 void clean(); //清空 39 40 41 StackNode<T> *head; 42 43 void printStack(); //打印栈数据 44 int length ; 45 }; 46 47 template<class T> 48 Stack<T>::Stack() 49 { 50 head = NULL; 51 length = 0; 52 } 53 54 template<class T> 55 Stack<T>::~Stack() 56 { 57 58 } 59 60 template<class T> 61 Stack<T>::Stack(const Stack& stack) 62 { 63 head = NULL; 64 length = 0; 65 66 StackNode< T> *tmp = stack.head; 67 68 while(tmp !=NULL) 69 { 70 71 StackNode<T> *stackNode = new StackNode<T>(); 72 stackNode->node = tmp->node; 73 stackNode->next = head; 74 head = stackNode; 75 76 length+=1; 77 78 tmp = tmp->next; 79 80 } 81 } 82 83 84 template<class T> 85 void Stack<T>::push(const T& item) 86 { 87 StackNode<T> *stackNode = new StackNode<T>(); 88 stackNode->node = item; 89 stackNode->next = head; 90 91 head =stackNode; 92 93 length+=1; 94 95 } 96 97 template<class T> 98 StackNode<T> *Stack<T>::popup() 99 { 100 101 if(head !=NULL) 102 { 103 StackNode<T> *tmp = head; 104 105 head = head->next; 106 107 length -=1; 108 109 return tmp; 110 } 111 else 112 { 113 return NULL; 114 } 115 } 116 117 template<class T> 118 bool Stack<T>::isEmpty() 119 { 120 if(head == NULL) 121 { 122 return true; 123 } 124 else 125 { 126 return false; 127 } 128 } 129 130 template <class T> 131 int Stack<T>::getLength() 132 { 133 if(length<0) 134 { 135 length = 0; 136 return 0; 137 } 138 else 139 { 140 return length; 141 } 142 } 143 144 template<class T> 145 void Stack<T>::clean() 146 { 147 if(head !=NULL) 148 { 149 StackNode<T> *tmp = head; 150 StackNode<T> *current; 151 152 while(tmp !=NULL) 153 { 154 current = tmp; 155 tmp = tmp->next; 156 157 delete current; 158 } 159 } 160 } 161 162 template<class T> 163 void Stack<T>::printStack() 164 { 165 StackNode<T> *tmp = head; 166 167 if(tmp !=NULL) 168 { 169 while(tmp !=NULL) 170 { 171 cout<<tmp->node<<endl; 172 173 tmp = tmp->next; 174 } 175 } 176 } 177 178 int main() 179 { 180 181 Stack<int > *stack = new Stack<int>(); 182 183 for(int i=0;i<10;i++) 184 { 185 stack->push(i); 186 } 187 188 stack->printStack(); 189 190 StackNode<int> *tmp = stack->popup(); 191 192 if(tmp !=NULL) 193 { 194 cout<<tmp->node<<endl; 195 delete tmp; 196 tmp = NULL; 197 198 } 199 else 200 { 201 cout<<"123"<<endl; 202 tmp = NULL; 203 } 204 205 tmp = stack->popup(); 206 207 if(tmp !=NULL) 208 { 209 cout<<tmp->node<<endl; 210 delete tmp; 211 tmp = NULL; 212 } 213 else 214 { 215 cout<<"123"<<endl; 216 tmp = NULL; 217 } 218 219 if(stack->isEmpty()) 220 { 221 cout<<"empty "<<endl; 222 } 223 else 224 { 225 cout<<"NoEmpty "<<endl; 226 } 227 228 cout<<stack->getLength()<<endl; 229 230 cout<<"*************************************"<<endl; 231 232 Stack<int>& a = *stack; 233 Stack<int> *stack1 = new Stack<int>(a); 234 235 cout<<stack1->getLength()<<endl; 236 237 stack1->printStack(); 238 239 stack->clean(); 240 stack1->clean(); 241 242 }