纪念逝去的岁月——C++实现一个队列(使用类模板)

1、代码

2、运行结果


 

1、代码

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 template <typename T> class ClsQueueData
  5 {
  6     private :
  7         ClsQueueData * __m_next;
  8         T * __m_Data;
  9 
 10     protected :
 11         void _clear();
 12 
 13     public :
 14         ClsQueueData(T * pData);
 15         ClsQueueData();
 16         ~ClsQueueData();
 17         void destroy();
 18 
 19         ClsQueueData * getNext();
 20         int setNext(ClsQueueData * p);
 21 
 22         T * getData();
 23         int setData(T * pData);
 24 };
 25 
 26 template <typename T> ClsQueueData<T>::ClsQueueData()
 27 {
 28     __m_next = NULL;
 29     __m_Data = NULL;
 30 }
 31 
 32 template <typename T> ClsQueueData<T>::ClsQueueData(T * p)
 33 {
 34     __m_next = NULL;
 35     __m_Data = p;
 36 }
 37 
 38 template <typename T> ClsQueueData<T>::~ClsQueueData()
 39 {
 40     _clear();
 41 }
 42 
 43 template <typename T> ClsQueueData<T> * ClsQueueData<T>::getNext()
 44 {
 45     return __m_next;
 46 }
 47 
 48 template <typename T> int ClsQueueData<T>::setNext(ClsQueueData * p)
 49 {
 50     if(NULL == p)
 51     {
 52         return -1;
 53     }
 54     p->__m_next = NULL;
 55     __m_next = p;
 56     
 57     return 0;
 58 }
 59 
 60 template <typename T> T * ClsQueueData<T>::getData()
 61 {
 62     return __m_Data;
 63 }
 64 
 65 template <typename T> int ClsQueueData<T>::setData(T * pData)
 66 {
 67     __m_Data = pData;
 68     return 0;
 69 }
 70 
 71 template <typename T> void ClsQueueData<T>::_clear()
 72 {
 73     if(NULL != __m_Data)
 74     {
 75         delete __m_Data;
 76     }
 77     __m_next = NULL;
 78 }
 79 
 80 template <typename T> void ClsQueueData<T>::destroy()
 81 {
 82     __m_Data = NULL;
 83     __m_next = NULL;
 84     delete this;
 85 }
 86 
 87 template <typename T> class ClsQueue
 88 {
 89     private :
 90         ClsQueueData<T> * __m_head;
 91         ClsQueueData<T> * __m_tail;
 92         size_t  __m_size;
 93 
 94     public :
 95         ClsQueue();
 96         ~ClsQueue();
 97 
 98         int  push(T * pData);
 99         int  pop (T ** pData);
100         void clear();
101         size_t getSize();
102 };
103 
104 template <typename T> ClsQueue<T>::ClsQueue()
105 {
106     __m_head = NULL;
107     __m_tail = NULL;
108     __m_size = 0;
109 }
110 
111 template <typename T> ClsQueue<T>::~ClsQueue()
112 {
113     __m_size = 0;
114     ClsQueueData<T> * p = __m_head;
115     while(p != NULL)
116     {
117         ClsQueueData<T> * x = p;
118         p = p->getNext();
119         delete x;
120     }
121     __m_head = NULL;
122     __m_tail = NULL;
123 }
124 
125 template <typename T> int  ClsQueue<T>::push(T * pData)
126 {
127     ClsQueueData<T> * pElm = new ClsQueueData<T>;
128     if(NULL == pElm)
129     {
130         return -1;
131     }
132     pElm->setData(pData);
133     if(NULL == __m_head)
134     {
135         __m_head = pElm;
136         __m_tail = pElm;
137     }
138     else if(NULL == __m_tail)
139     {
140         ClsQueueData<T> * p = __m_head;
141         while(p->getNext() != NULL)
142         {
143             p = p->getNext();
144         }
145         __m_tail = p;
146     }
147     else
148     {
149         __m_tail->setNext(pElm);
150         __m_tail = pElm;
151     }
152     __m_size ++;
153 
154     return 0;
155 }
156 
157 template <typename T> int  ClsQueue<T>::pop (T ** pData)
158 {
159     if(NULL == __m_head || NULL == pData)
160     {
161         return -1;
162     }
163     ClsQueueData<T> * p = __m_head;
164 
165     if(__m_head == __m_tail)
166     {
167         __m_head = __m_tail = NULL;
168     }
169     else
170     {
171         __m_head = __m_head->getNext();
172     }
173     __m_size --;
174     *pData = p->getData();
175     p->destroy();
176 
177     return 0;
178 }
179 
180 template <typename T> void ClsQueue<T>::clear()
181 {
182     ClsQueueData<T> * p = __m_head;
183     while(p != NULL)
184     {
185         ClsQueueData<T> * x = p;
186         p = p->getNext();
187         delete x;
188     }
189     __m_head = __m_tail = NULL;
190     __m_size = 0;
191 }
192 
193 template <typename T> size_t ClsQueue<T>::getSize()
194 {
195     return __m_size;
196 }
197 
198 int main()
199 {
200     ClsQueue<int> objQ;
201     int i = 0;
202     for(i = 0; i < 10; i++)
203     {
204         int * x = new int;
205         *x = i;
206         objQ.push(x);
207         printf("line : [%d]; queue size : [%u]\n", __LINE__, objQ.getSize());
208     }
209     for(i = 0; i < 11; i++)
210     {
211         int * x = NULL;
212         int iRet = objQ.pop(&x);
213         printf("line : [%d]; queue size : [%u]\n", __LINE__, objQ.getSize());
214         if(-1 == iRet)
215         {
216             printf("get data faild\n");
217             continue;
218         }
219         if(i == *x)
220         {
221             printf("RIGHT\n");
222         }
223         else
224         {
225             printf("WRONG\n");
226         }
227         delete x;
228     }
229     for(i = 0; i < 10; i++)
230     {
231         int * x = new int;
232         *x = i;
233         objQ.push(x);
234     }
235     printf("line : [%d]; queue size : [%u]\n", __LINE__, objQ.getSize());
236     objQ.clear();
237     printf("line : [%d]; queue size : [%u]\n", __LINE__, objQ.getSize());
238 
239     return 0;
240 }

2、运行结果

  2.1)编译

g++ -g    -c -o queue.o queue.cpp
g++ -g -o queue queue.o

  2.2)运行结果

$ ./queue 
line : [207]; queue size : [1]
line : [207]; queue size : [2]
line : [207]; queue size : [3]
line : [207]; queue size : [4]
line : [207]; queue size : [5]
line : [207]; queue size : [6]
line : [207]; queue size : [7]
line : [207]; queue size : [8]
line : [207]; queue size : [9]
line : [207]; queue size : [10]
line : [213]; queue size : [9]
RIGHT
line : [213]; queue size : [8]
RIGHT
line : [213]; queue size : [7]
RIGHT
line : [213]; queue size : [6]
RIGHT
line : [213]; queue size : [5]
RIGHT
line : [213]; queue size : [4]
RIGHT
line : [213]; queue size : [3]
RIGHT
line : [213]; queue size : [2]
RIGHT
line : [213]; queue size : [1]
RIGHT
line : [213]; queue size : [0]
RIGHT
line : [213]; queue size : [0]
get data faild
line : [235]; queue size : [10]
line : [237]; queue size : [0]

 

posted @ 2015-06-04 20:52  fengbohello  阅读(2704)  评论(0编辑  收藏  举报