练队列 类模板实现

//..................................
//linkqueue
#include<iostream>
using namespace std;


//结构 QNode
//.......................
template<class T>
struct QNode{
T  data;
QNode<T> *next;
};
//.......................


//类linkqueue
//.......................
template<class T>
class LinkQueue
{
public:
void CreateQueue();
void EnQueue(T e);
T Gettop();
T Deletetop();
int QueueLength();
void DestroyQueue();
void display();
void ClearQueue();
void EmptyQueue();








private:
QNode<T> *front;
QNode<T> *rear;
};
//.......................
//类LinkQueue 的实现
//.......................
template<class T>
void LinkQueue<T>::CreateQueue()
{
front=rear=(QNode<T> *)malloc(sizeof(QNode<T>));
if(!front)cout<<"false"<<endl;
front->next=NULL;
}


template<class T>
void LinkQueue<T>::EnQueue(T e)
{
QNode<T> *p;
p=(QNode<T> *)malloc(sizeof(QNode<T>));
if(!p)cout<<"false"<<endl;
p->data=e;
p->next=NULL;
rear->next=p;
rear=p;
}
template<class T>
T LinkQueue<T>::Gettop()
{
T e;
e=front->next->data;
return e;
}
template<class T>
T LinkQueue<T>::Deletetop()
{
T e;
QNode<T> *p;
p=front->next;
if(!p)cout<<"false"<<endl;
front->next=p->next;
e=p->data;
free(p);
return e;
}
template<class T>
int LinkQueue<T>::QueueLength()
{
QNode<T> *p;
p=front->next;
if(!p)cout<<"false"<<endl;
int j=0;
while(p)
{
p=p->next;
j++;
}
return j;


}
template<class T>
void LinkQueue<T>::DestroyQueue()
{
if(!front)cout<<"flase"<<endl;




while(front)
{
rear=front->next;
free(front);
front=rear;

}



}




template<class T>
void LinkQueue<T>::display()
{
QNode<T> *p;
p=front->next;
if(!p)cout<<"false"<<endl;
while(p)
{
cout<<p->data<<",";
p=p->next;
}
cout<<endl;
}
template<class T>
void LinkQueue<T>::ClearQueue()
{
QNode<T> *p;
rear=p=front->next;
if(!p)cout<<"false"<<endl;
while(p)
{
p=p->next;
free(rear);
rear=p;
}
rear=front;
}
template<class T>
void LinkQueue<T>::EmptyQueue()
{
if(front==rear)
cout<<"此队列为空!"<<endl;
else
cout<<"此队列不为空!"<<endl;
}



















//.......................
//main函数
//.......................
void main()
{
LinkQueue<int> Q1;
Q1.CreateQueue();
cout<<"输入要输入元素的个数:"<<endl;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
Q1.EnQueue(i);
}
cout<<Q1.Gettop()<<endl;
cout<<Q1.QueueLength()<<endl;
Q1.display();
cout<<Q1.Deletetop()<<endl;
Q1.ClearQueue();
Q1.EmptyQueue();



for(i=1;i<=n;i++)
{
Q1.EnQueue(i);
}
    Q1.DestroyQueue();




}


//.......................
//........................................................
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值