//鏈表描述的隊列類LinkedQueue#include <iostream>using namespace std;template <class T>class Node{public:T data;Node<T> *link;};//FIIFO對象template <class T>class LinkedQueue{public:LinkedQueue(){front=rear=0;}~LinkedQueue();bool IsEmpty()const{return ((front)?false:true);}bool IsFull()const;T First()const;//返回隊首元素T Last()const;//返回隊尾元素LinkedQueue<T>& Add(const T& x);LinkedQueue<T>& Delete(T& x);void Output();Node<T> *front;//指向第一個節點Node<T> *rear;//最後一個節點};template <class T>LinkedQueue<T>::~LinkedQueue(){Node<T> *next;while (front){next = front->link;delete front;front = next;}}class OutOfBounds{ public: OutOfBounds(){ cout<<"Out Of Bounds!"<<endl; } }; //記憶體不足的異常類 class NoMem{ public: NoMem(){ cout<<"No Memory!"<<endl; } }; //該隊列的輸出方法template<class T>void LinkedQueue<T>::Output(){Node<T> *temp = front;while(temp){cout<<temp->data<<" ";temp=temp->link;}cout<<""<<endl;}//判斷當前隊列是否已滿template <class T>bool LinkedQueue<T>::IsFull()const{Node<T>*p;try{p = new Node<T>;delete p;return false;}catch (NoMem){return true;}}//返回隊列的第一個元素template <class T>T LinkedQueue<T>::First()const{if(IsEmpty())throw OutOfBounds();return front->data}//返回隊列的最後一個元素template <class T>T LinkedQueue<T>::Last()const{if(IsEmpty())throw OutOfBounds();return rear->data}//將x添加到隊列的尾部template<class T>LinkedQueue<T>& LinkedQueue<T>::Add(const T&x){Node<T>*p = new Node<T>;p->data=x;p->link=0;//在隊列尾部添加新節點if (front){rear->link = p;} else{front=p;}rear=p;return *this;}//刪除第一個元素並將其放到X中去template<class T>LinkedQueue<T>& LinkedQueue<T>::Delete(T&x){if (IsEmpty()){throw OutOfBounds();}x = front->data;Node<T>*p = front;front = front->link;delete p;return *this;}void main(){LinkedQueue<int>myQueue;myQueue.Add(5);myQueue.Add(2);myQueue.Add(0);myQueue.Add(1);myQueue.Add(3);myQueue.Add(1);myQueue.Add(4);myQueue.Output();int temp;myQueue.Delete(temp);myQueue.Delete(temp);myQueue.Delete(temp);myQueue.Output();}