#include<iostream>
using namespace std;
//定义结点类型
template<class T> //T为虚拟类型
struct node
{
T d;
node *next;
};
//定义带链队列类
template<class T> //模板声明,数据元素虚拟类型为T
class linked_Queue
{
private: //数据成员
node<T> *front; //带链队列排头指针
node<T> *rear; //带链队列队尾指针
public: //成员函数
linked_Queue(); //构造函数,建立空队列,即队列初始化
void prt_linked_Queue(); //顺序输出带链队列中的元素
int flag_linked_Queue(); //检测带链队列的状态
void ins_linked_Queue(T); //入队
T del_linked_Queue(); //退队
};
//带链队列初始化
template<class T>
linked_Queue<T>::linked_Queue()
{
front=NULL; //排头指针与队尾指针均为空
rear=NULL;
return;
}
//顺序输出队列中的元素
template<class T>
void linked_Queue<T>::prt_linked_Queue()
{
node<T> *p;
p=front;
if(p=NULL)
{
cout<<"空队列!"<<endl;
return;
}
do
{
cout<<p->d<<endl;
p=p->next;
} while (p!=NULL);
}
//检测带链队列的状态
template<class T>
int linked_Queue<T>::flag_linked_Queue()
{
if(front=NULL)return(0); //若带链的队列为空,则函数返回0
return(1); //正常函数返回1
}
//入队
template<class T>
void linked_Queue<T>::ins_linked_Queue(T x)
{
node<T> *p;
p=new node<T> //申请一个新结点
p->d=x; //置新结点数据域值
p->next=NULL; //置新结点指针域值为空
if(rear==NULL) //原队列为空
front=p;
else
rear->next=p; //原队尾结点的指针指向新结点
rear=p; //队尾指针指向新结点
return;
}
//退队
template<class T>
T linked_Queue<T>::del_linked_Queue()
{
T y;
node<T> *q;
if(front==NULL)
{
cout<<"空队!"<<endl;
return(0);
}
y=front->d //排头元素赋给变量
q=front;
front=q->next; //排头指针指向下一个结点
delete q; //释放结点空间
if(front==NULL)rear=NULL; //队列已空
return(y) //返回退队的元素
}