用类模板和双向链表实现的
加油!
要找到好工作!
这是stack.h
1 #ifndef STACK_H 2 #define STACK_H 3 4 /*************************************** 5 *********双链表栈 C++模块的实现********** 6 ***************************************/ 7 // ref: http://blog.chinaunix.net/uid-21206300-id-3164886.html 8 9 #include <iostream> 10 using namespace std; 11 12 namespace MY_STACK 13 { 14 //====================================================// 15 // Node: 16 template <typename T> 17 class Node 18 { 19 public: 20 Node() 21 { 22 this->element = (T)0;//0; // TODO: 等于0?如果是指针或对象呢? 23 this->next = NULL; 24 this->prev = NULL; 25 } 26 ~Node() 27 { 28 this->element = (T)0;//0; 29 this->next = NULL; 30 this->prev = NULL; 31 } 32 T &getElement() 33 { 34 return this->element; 35 } 36 37 //重载"<<"操作符,以便对栈节点输出,注意以friend重载“<<”操作符要加上"<>" 38 friend ostream &operator << <>(ostream &ost, Node<T> &src) 39 { 40 ost << src.getElement(); 41 return ost; 42 } 43 44 Node<T> *next; 45 Node<T> *prev; 46 template <typename T> friend class Stack; 47 48 protected: 49 T element; 50 51 }; 52 53 //=====================================================// 54 // Stack: 55 template <typename T> 56 class Stack 57 { 58 public: 59 Stack(); 60 ~Stack(); 61 62 bool push(const T &element); 63 T pop(); 64 65 bool isEmpty(); 66 67 bool clear(); 68 69 int size(); 70 71 friend ostream &operator << <>(ostream &ost, Stack<T> &src); 72 73 protected: 74 Node<T> *createNode(const T &element); 75 76 protected: 77 Node<T> *head;// 链表头为head 78 Node<T> *top; // 链表尾为top 79 80 private: 81 int count; 82 }; 83 84 //=====================================================// 85 // Stack realize: 86 template <typename T> 87 Stack<T>::Stack() 88 { 89 T node = T(); 90 // 创建一个带有头节点的双链栈,这个节点不存入任何数据 91 this->head = this->createNode(node); 92 this->top = this->head; 93 this->count = 0; 94 } 95 96 template <typename T> 97 Stack<T>::~Stack() 98 { 99 this->clear(); 100 delete this->head; 101 this->head = NULL; 102 this->top = NULL; 103 } 104 105 template <typename T> 106 Node<T> *Stack<T>::createNode(const T &element) 107 { 108 Node<T> *p = new Node<T>; 109 if (p) 110 { 111 p->element = element; 112 p->next = NULL; 113 p->prev = NULL; 114 } 115 return p; 116 } 117 118 template <typename T> 119 bool Stack<T>::push(const T &element) 120 { 121 Node<T> *p = this->createNode(element); 122 if (NULL == this->head->next) 123 { 124 this->head->next = p; 125 p->prev = this->head; 126 } 127 else 128 { 129 this->top->next = p; 130 p->prev = this->top; 131 } 132 this->count++; 133 // 把链表尾当作top 134 this->top = p; 135 136 return true; 137 } 138 139 template <typename T> 140 T Stack<T>::pop() 141 { 142 // if is not empty 143 if (!isEmpty()) 144 { 145 Node<T> *p = this->top; 146 this->top = this->top->prev; 147 //this->top->next = NULL; // TODO: test 148 149 T elem = p->element; // 要注意浅拷贝! 150 //T elem = p->getElement(); 151 152 p->next = p->prev = NULL; 153 delete p; 154 p = NULL; 155 156 this->count--; 157 158 return elem; 159 } 160 return (T)0; 161 } 162 163 template <typename T> 164 bool Stack<T>::isEmpty() 165 { 166 return (this->head == this->top); 167 } 168 169 template <typename T> 170 bool Stack<T>::clear() 171 { 172 Node<T> *p = this->top; 173 while ((p = this->top) != this->head) 174 { 175 this->top = p->prev; 176 //this->top->next = NULL; // TODO: test 177 p->next = p->prev = NULL; 178 delete p; 179 p = NULL; 180 } 181 // 或者是: 182 /*while (!isEmpty()) 183 this->pop();*/ 184 this->count = 0; 185 186 return true; 187 } 188 189 template <typename T> 190 int Stack<T>::size() 191 { 192 return this->count; 193 } 194 195 template <typename T> 196 ostream &operator << <>(ostream &ost, Stack<T> &src) 197 { 198 Node<T> *p = src.top; 199 while (p != src.head) 200 { 201 ost << *p << " "; 202 p = p->prev; 203 } 204 return ost; 205 } 206 } 207 208 209 #endif
这是main:
1 #include "stack.h" 2 3 using namespace MY_STACK; 4 5 int main() { 6 Stack<int> s; 7 s.push(1); 8 s.push(2); 9 s.push(3); 10 s.push(4); 11 s.push(5); 12 13 cout << s << endl; 14 cout << s.size() << endl; 15 16 s.pop(); 17 18 cout << s << endl; 19 cout << s.size() << endl; 20 21 s.pop(); 22 s.pop(); 23 s.pop(); 24 s.pop(); 25 s.pop(); 26 27 cout << s << endl; 28 cout << s.size() << endl; 29 30 //============================// 31 Stack<char> s1; 32 s1.push('a'); 33 s1.push('b'); 34 s1.push('c'); 35 cout << s1 << endl; 36 cout << s1.size() << endl; 37 38 s1.clear(); 39 cout << s1 << endl; 40 cout << s1.size() << endl; 41 42 return 0; 43 }
本人的环境:vs2013
弦上故人来,纸底乱飞红。