//..................................
//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();
}
//.......................
//........................................................
//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();
}
//.......................
//........................................................