链队列类源码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma once template <class T> class QueueNode { public: T element; QueueNode<T> *next; }; template <class T> class LinkQueue { public: LinkQueue(void) { front = new QueueNode<T>(); rear = new QueueNode<T>(); front->next = NULL; rear->next = front; } void in(T obj) { QueueNode<T> *newNode = new QueueNode<T>(); newNode->element = obj; newNode->next = NULL; rear->next->next = newNode; rear->next = newNode; } T out() { if(!IsEmpty()) { QueueNode<T> *topNode = front->next; if(topNode->next==NULL) { //此时topNode为最后一个节点,front和rear的next都指向它,取出topNode后队列变为空,链队变回到初始状态 front->next = NULL; rear->next = front; } else front->next = topNode->next; T temp = topNode->element; delete(topNode); return temp; } else return NULL; } bool IsEmpty() { if(front->next==NULL) return true; else return false; } ~LinkQueue(void) { } private: QueueNode<T> *front; QueueNode<T> *rear; };
测试代码:
//链队列 LinkQueue<int> * queue2 = new LinkQueue<int>(); queue2->in(3);queue2->in(2);queue2->in(7);queue2->in(6); queue2->in(8);queue2->in(5);queue2->in(4);queue2->in(1); while(!queue2->IsEmpty()) { std::cout<<queue2->out()<<" "; }