标签:
Queue.h头文件
1 /************************************************************************* 2 > File Name: queue.h 3 > Author: MarkWoo 4 > Mail:wcgwuxinwei@gmail.com 5 > Created Time: 2015年04月23日 星期四 14时10分03秒 6 ************************************************************************/ 7 8 #ifndef _QUEUE_H_ 9 #define _QUEUE_H_ 10 11 #include <iostream> 12 13 template<class Type> 14 class Queue; 15 16 template<class Type> 17 std::ostream& 18 operator<<(std::ostream &ost, const Queue<Type> &queue_output); // standard output stream, overload behavior must be non-member function 19 20 // represent element of Queue, realize by using linklist 21 template<class Type> 22 class QueueItems { 23 friend class Queue<Type>; //because of members of QueueItems are private, and Queue need to visit them, so we define class queue as friend class 24 friend std::ostream& 25 operator<< <Type> (std::ostream &ost, const Queue<Type> &queue_output); // as friend template function, make one single instance (called "specialization" in generic terms) of that template a friend 26 Type data_; // data of queue item 27 QueueItems<Type> *next_; // pointer of next element 28 }; 29 30 // Queue 31 template<class Type> 32 class Queue { 33 public: 34 //template<class T>friend std::ostream& 35 friend std::ostream& 36 operator<< <Type> (std::ostream &ost, const Queue<Type> &queue_output); // as friend template function, make one single instance (called "specialization" in generic terms) of that template a friend 37 38 // constuctor of queue, and in fact, this expression 39 // equal to Queue<Type>, and the following member function also like this 40 Queue(): head_(NULL), tail_(NULL) { 41 } 42 // copy constuctor, copy contorl 43 Queue(const Queue<Type> &queue_): head_(queue_.head_), tail_(queue_.tail_) { 44 } 45 template<class It> 46 Queue(It beg, It end) { 47 copy_elems(beg, end); 48 } 49 Queue &operator=(const Queue<Type> &queue_raw) { 50 if (&queue_raw == this) { 51 return ; 52 } else { 53 copy_elems(queue_raw); 54 } 55 } 56 ~Queue() { 57 destory(); 58 } 59 60 const Type &front() const; //as well as above, only for const situation 61 template <class Iter> void assign(Iter, Iter); //copy a entire queue to new queue 62 Type &front(); // get a element from tail postion of queue 63 void push(const Type &elem); //push a element into current queue 64 void pop(); // remove a element from tail postion of queue 65 bool empty() const; // queue whether empty 66 private: 67 QueueItems<Type> *head_; // linklist of head 68 QueueItems<Type> *tail_; // linklist of tail 69 void destory(); 70 void copy_elems(const Queue<Type> &queue_raw); // copy 71 template<class Iter> void copy_elems(Iter begin, Iter end); // iter is iterator type, this function accept iterator arguement 72 }; 73 74 #endif //queue.h
Queue.cc 实现文件
1 /************************************************************************* 2 > File Name: queue.cc 3 > Author: MarkWoo 4 > Mail:wcgwuxinwei@gmail.com 5 > Created Time: 2015年04月24日 星期五 11时27分07秒 6 ************************************************************************/ 7 8 #include "queue.h" 9 #include <string> 10 11 using namespace std; 12 13 template<class Type> 14 std::ostream& 15 operator<<(std::ostream &ost, const Queue<Type> &queue_output) { 16 QueueItems<Type> *tmp = queue_output.head_; 17 18 for ( ; tmp != NULL; tmp = tmp->next_) { 19 ost << tmp->data_ << " "; 20 } 21 22 return ost; 23 } 24 25 template<class Type> 26 template<class Iter> 27 void Queue<Type>::assign(Iter begin, Iter end) { 28 if (begin == end) { 29 return ; 30 } else { 31 destory(); 32 copy_elems(begin, end); 33 } 34 } 35 36 template<class Type> 37 void Queue<Type>::push(const Type &elem) { 38 QueueItems<Type> *tmp = new QueueItems<Type>; 39 tmp->data_ = elem; 40 41 if (head_ == NULL) { 42 head_ = tail_ = tmp; 43 tmp->next_ = NULL; 44 } else { 45 tmp->next_ = tail_; 46 tail_ = tmp; 47 } 48 } 49 50 template<class Type> 51 const Type &Queue<Type>::front() const { 52 return head_->data_; 53 } 54 55 template<class Type> 56 Type &Queue<Type>::front() { 57 return head_->data_; 58 } 59 60 template<class Type> 61 void Queue<Type>::pop() { 62 QueueItems<Type> *tmp = head_; 63 head_ = head_->next_; 64 delete tmp; 65 tmp = NULL; 66 } 67 68 template<class Type> 69 void Queue<Type>::destory() { 70 71 if (empty()) { 72 return ; 73 } else { 74 for (; head_ != NULL; head_ = head_->next_) { 75 pop(); 76 } 77 head_ = tail_ = NULL; 78 } 79 80 } 81 82 template<class Type> 83 bool Queue<Type>::empty() const { 84 return head_ ? true : false; 85 } 86 87 template<class Type> 88 void Queue<Type>::copy_elems(const Queue<Type> &queue_raw) { 89 if (queue_raw->head_ == NULL) { 90 return ; 91 } else { 92 93 destory(); 94 QueueItems<Type> *tmp = queue_raw.next_; 95 for (; tmp != NULL; tmp = tmp->next_) { 96 push(tmp->data_); 97 } 98 } 99 } 100 101 template<class Type> 102 template<class Iter> 103 void Queue<Type>::copy_elems(Iter begin, Iter end) { 104 if (begin == end) { 105 } else { 106 for (; begin != end; begin++) { 107 push(*begin); 108 } 109 } 110 } 111 112 int main(void) { 113 Queue<int> sample_string_queue; 114 int a = 10; 115 sample_string_queue.push(a); 116 std::cout << sample_string_queue << std::endl; 117 return 0; 118 }
标签:
原文地址:http://www.cnblogs.com/MarkWoo/p/4462655.html