标签:
写了个栈和队列以及简化的deque模版。
Stack:
1 #ifndef _KIRAI_STACK 2 #pragma once 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class Type> 8 struct Node { 9 typedef Node<Type>* NP; 10 Type data; 11 NP pre; 12 Node<Type>() { pre = NULL; } 13 }; 14 15 template <class Type> 16 class stack { 17 typedef Node<Type>* NP; 18 typedef Node<Type> NT; 19 20 public: 21 stack<Type>() { _top = NULL; _size = 0; } 22 ~stack<Type>() { clear(); } 23 24 public: 25 bool push(Type); 26 Type pop(); 27 void clear(); 28 bool empty() const; 29 int size() const; 30 Type top() const; 31 32 protected: 33 NP _top; 34 int _size; 35 36 private: 37 void _init(Type); 38 void _clear(NP); 39 }; 40 41 template <class Type> 42 Type stack<Type>::top() const { 43 return _top->data; 44 } 45 template <class Type> 46 bool stack<Type>::empty() const { 47 return _size == 0 ? true : false; 48 } 49 50 template <class Type> 51 void stack<Type>::_clear(NP cur) { 52 if (cur == NULL) { 53 return; 54 } 55 _clear(cur->pre); 56 delete cur; 57 } 58 59 template <class Type> 60 int stack<Type>::size() const { 61 return _size; 62 } 63 64 template <class Type> 65 void stack<Type>::_init(Type val) { 66 if (_top != NULL) { 67 clear(); 68 } 69 _top = new NT; 70 _top->data = val; 71 _size++; 72 } 73 74 template <class Type> 75 bool stack<Type>::push(Type val) { 76 if (_size == 0) { 77 _init(val); 78 return true; 79 } 80 NP tmp = new NT; 81 if (tmp == NULL) { 82 return false; 83 } 84 tmp->data = val; 85 tmp->pre = _top; 86 _top = tmp; 87 _size++; 88 return true; 89 } 90 91 template <class Type> 92 Type stack<Type>::pop() { 93 if (empty()) { 94 exit(EXIT_FAILURE); 95 } 96 NP cur = _top; 97 Type tmp = cur->data; 98 _top = _top->pre; 99 delete cur; 100 _size--; 101 return tmp; 102 } 103 104 template<class Type> 105 void stack<Type>::clear() { 106 _clear(_top); 107 _top = NULL; 108 _size = 0; 109 } 110 } 111 #endif
Queue:
1 #ifndef _KIRAI_QUEUE 2 #pragma once 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class Type> 8 struct Node { 9 typedef Node<Type>* NP; 10 Type data; 11 NP next; 12 int pos; 13 Node<Type>() { next = NULL; } 14 }; 15 16 template <class Type> 17 class queue { 18 typedef Node<Type>* NP; 19 typedef Node<Type> NT; 20 21 public: 22 queue<Type>() { head = NULL; tail = NULL; _size = 0; } 23 ~queue<Type>() { clear(); } 24 25 public: 26 bool push_back(Type); 27 Type pop_front(); 28 void clear(); 29 bool empty() const; 30 int size() const; 31 Type front() const; 32 Type back() const; 33 protected: 34 NP head; 35 NP tail; 36 37 private: 38 int _size; 39 void _init(Type); 40 void _clear(NP); 41 }; 42 43 template <class Type> 44 void queue<Type>::_clear(NP cur) { 45 if (cur == NULL) { 46 return; 47 } 48 _clear(cur->next); 49 delete cur; 50 } 51 52 template <class Type> 53 Type queue<Type>::front() const { 54 return head->data; 55 } 56 57 template <class Type> 58 Type queue<Type>::back() const { 59 return tail->data; 60 } 61 62 template <class Type> 63 bool queue<Type>::empty() const { 64 return _size == 0 ? true : false; 65 } 66 67 template <class Type> 68 int queue<Type>::size() const { 69 return _size; 70 } 71 72 template <class Type> 73 void queue<Type>::_init(Type val) { 74 if (head != NULL) { 75 clear(); 76 } 77 head = new NT; 78 head->data = val; 79 tail = head; 80 _size++; 81 } 82 83 template <class Type> 84 bool queue<Type>::push_back(Type val) { 85 if (_size == 0) { 86 _init(val); 87 return true; 88 } 89 NP tmp = new NT; 90 if (tmp == NULL) { 91 return false; 92 } 93 tmp->data = val; 94 while (tail->next != NULL) { 95 tail = tail->next; 96 } 97 tail->next = tmp; 98 tail = tail->next; 99 _size++; 100 return true; 101 } 102 103 template<class Type> 104 Type queue<Type>::pop_front() { 105 if (empty()) { 106 exit(EXIT_FAILURE); 107 } 108 NP cur = head; 109 Type tmp = cur->data; 110 head = head->next; 111 delete cur; 112 _size--; 113 return tmp; 114 } 115 116 template<class Type> 117 void queue<Type>::clear() { 118 _clear(head); 119 head = NULL; 120 _size = 0; 121 } 122 } 123 #endif
Deque:
1 #ifndef _KIRAI_DEQUE 2 #pragma once 3 4 #include <cstdlib> 5 6 namespace kirai { 7 template <class Type> 8 struct Node { 9 typedef Node<Type>* NP; 10 Type data; 11 NP pre; 12 NP next; 13 int pos; 14 Node<Type>() { pre = NULL; next = NULL; } 15 }; 16 17 template <class Type> 18 class deque { 19 typedef Node<Type>* NP; 20 typedef Node<Type> NT; 21 22 public: 23 deque<Type>() { head = NULL; tail = NULL; _size = 0; } 24 ~deque<Type>() { clear(); } 25 26 public: 27 bool push_back(Type); 28 bool push_front(Type); 29 Type pop_back(); 30 Type pop_front(); 31 void clear(); 32 bool empty() const; 33 int size() const; 34 Type front() const; 35 Type back() const; 36 protected: 37 NP head; 38 NP tail; 39 40 private: 41 int _size; 42 void _init(Type); 43 }; 44 45 template <class Type> 46 Type deque<Type>::front() const { 47 return head->data; 48 } 49 50 template <class Type> 51 Type deque<Type>::back() const { 52 return tail->data; 53 } 54 55 template <class Type> 56 bool deque<Type>::empty() const { 57 return _size == 0 ? true : false; 58 } 59 60 template <class Type> 61 int deque<Type>::size() const { 62 return _size; 63 } 64 65 template <class Type> 66 void deque<Type>::_init(Type val) { 67 if (head != NULL) { 68 clear(); 69 } 70 head = new NT; 71 head->data = val; 72 tail = head; 73 _size++; 74 } 75 76 template <class Type> 77 bool deque<Type>::push_back(Type val) { 78 if (_size == 0) { 79 _init(val); 80 return true; 81 } 82 NP tmp = new NT; 83 if (tmp == NULL) { 84 return false; 85 } 86 tmp->data = val; 87 tmp->pre = tail; 88 tail = tmp; 89 _size++; 90 return true; 91 } 92 93 template <class Type> 94 bool deque<Type>::push_front(Type val) { 95 if (empty()) { 96 _init(val); 97 return true; 98 } 99 NP tmp = new NT; 100 if (tmp == NULL) { 101 return false; 102 } 103 tmp->data = val; 104 tmp->next = head; 105 head = tmp; 106 _size++; 107 return true; 108 } 109 110 template <class Type> 111 Type deque<Type>::pop_back() { 112 if (empty()) { 113 exit(EXIT_FAILURE); 114 } 115 NP cur = tail; 116 Type tmp = cur->data; 117 tail = tail->pre; 118 delete cur; 119 _size--; 120 return tmp; 121 } 122 123 template<class Type> 124 Type deque<Type>::pop_front() { 125 if (empty()) { 126 exit(EXIT_FAILURE); 127 } 128 NP cur = head; 129 Type tmp = cur->data; 130 head = head->next; 131 delete cur; 132 _size--; 133 return tmp; 134 } 135 136 template <class Type> 137 void deque<Type>::clear() { 138 if (_size == 0) { 139 return; 140 } 141 NP cur = tail; 142 NP tmp = tail; 143 while (cur->next != NULL) { 144 cur = cur->pre; 145 delete tmp; 146 tmp = cur; 147 } 148 delete cur->pre; 149 _size = 0; 150 } 151 } 152 #endif
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4801932.html