LinkQueue(链队列)

关于Node.h,请参考LinkStack

  1 #include"Node.h"
  2 template<typename ElemType>
  3 class LinkQueue
  4 {
  5 protected:
  6     Node<ElemType> *front,*rear;
  7     int count;
  8 public:
  9     LinkQueue();
 10     virtual ~LinkQueue();
 11     int Length()const;
 12     bool Empty()const;
 13     void Clear();
 14     bool Inqueue(const ElemType &e);
 15     bool Outqueue(ElemType &e);
 16     bool GetHead(ElemType &e) const;
 17     void Traverse(void(*visit)(const ElemType &))const;
 18     LinkQueue(const LinkQueue<ElemType> &copy);
 19     LinkQueue<ElemType> &operator=(const LinkQueue<ElemType> &copy);
 20 };
 21 template<typename ElemType>
 22 //构造一个空队列;
 23 LinkQueue<ElemType>::LinkQueue()
 24 {
 25     front=rear=new Node<ElemType>;
 26     count=0;
 27 }
 28 template<typename ElemType>
 29 //虚虚构函数
 30 LinkQueue<ElemType>::~LinkQueue()
 31 {
 32     Clear();
 33 }
 34 template<typename ElemType>
 35 //返回队列长度
 36 int LinkQueue<ElemType>::Length()const
 37 {
 38     return count;
 39 }
 40 template<typename ElemType>
 41 //判断队列为空
 42 bool LinkQueue<ElemType>::Empty()const
 43 {
 44     return count==0;
 45 }
 46 template<typename ElemType>
 47 //清除队列
 48 void LinkQueue<ElemType>::Clear()
 49 {
 50     ElemType tmpElem;
 51     while(!Empty())
 52         Outqueue(tmpElem);
 53 }
 54 template<typename ElemType>
 55 //队尾进队
 56 bool LinkQueue<ElemType>::Inqueue(const ElemType &e)
 57 {
 58     Node<ElemType> *tmpPtr=new Node<ElemType>(e);
 59     rear->next=tmpPtr;
 60     rear=tmpPtr;
 61     count++;
 62     return true;
 63 
 64 }
 65 template<typename ElemType>
 66 //队头出队
 67 bool LinkQueue<ElemType>::Outqueue(ElemType &e)
 68 {
 69     if(!Empty())
 70     {
 71         Node<ElemType> *tmpPtr=front->next;
 72         e=tmpPtr->data;
 73         front->next=tmpPtr->next;
 74         if(rear==tmpPtr)
 75             rear=front;
 76         delete tmpPtr;
 77         count--;
 78         return true;
 79     }
 80     else
 81         return false;
 82 }
 83 template<typename ElemType>
 84 //返回队头元素
 85 bool LinkQueue<ElemType>::GetHead(ElemType &e) const
 86 {
 87     if(!Empty())
 88     {
 89         e=front->next->data;
 90         return true;
 91     }
 92     else
 93         return false;
 94 
 95 }
 96 template<typename ElemType>
 97 //visit
 98 void LinkQueue<ElemType>::Traverse(void(*visit)(const ElemType &))const
 99 {
100     for(Node<ElemType> *tmpPtr=front->next;tmpPtr;tmpPtr=tmpPtr->next)
101         (*visit)(tmpPtr->data);
102 }
103 template<typename ElemType>
104 //复制构造
105 LinkQueue<ElemType>::LinkQueue(const LinkQueue<ElemType> &copy)
106 {
107     rear=front=new Node<ElemType>;
108     count=0;
109     for(Node<ElemType> *tmpPtr=copy.front->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
110         Inqueue(tmpPtr->data);
111 }
112 template<typename ElemType>
113 //重载=operator
114 LinkQueue<ElemType> &LinkQueue<ElemType>::operator=(const LinkQueue<ElemType> &copy)
115 {
116     if(&copy!=this)
117     {
118         Clear();
119         for(Node<ElemType> *tmpPtr=copy.front->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
120             Inqueue(tmpPtr->data);
121         return *this;
122     }
123 
124 }
原文地址:https://www.cnblogs.com/ljwTiey/p/4268134.html