#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T>*next;
};
template<class T>
class LQ
{
public:
LQ();
~LQ();
void EQ(T x);
T DQ();
T GQ();
int Empty();
private:
Node<T>*front,*rear;
};
template<class T>
LQ<T>::LQ()
{
Node<T>*s=NULL;
s=new Node<T>;
s->next=NULL;
front=rear=s;
}
template<class T>
LQ<T>::~LQ()
{
Node<T>*p=NULL;
while(front!=NULL)
{
p=front->next;
delete front;
front=p;
}
}
template<class T>
void LQ<T>::EQ(T x)
{
Node<T>*s=NULL;
s=new Node<T>;
s->data=x;
s->next=NULL;
rear->next=s;rear=s;
}
template<class T>
T LQ<T>::DQ()
{
Node<T>*p=NULL;
int x;
if(rear==front)throw"下溢";
p=front->next;
x=p->data;
front->next=p->next;
if(p->next==NULL)rear=front;
delete p;
return x;
}
template<class T>
T LQ<T>::GQ()
{
if(front!=rear)
return front->next->data;
else
return 0;
}
template<class T>
int LQ<T>::Empty()
{
if(front==rear)
return 1;
else
return 0;
}
void main()
{
LQ<int>Q;
if(Q.Empty())
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
cout<<"元素15和20执行入队操作:"<<endl;
try
{
Q.EQ(15);
Q.EQ(20);
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"查看对头元素:"<<endl;
cout<<Q.GQ()<<endl;
cout<<"执行出对操作:"<<endl;
try
{
Q.DQ();
}
catch(char *wrong)
{
cout<<wrong<<endl;
}
cout<<"查看对头元素:"<<endl;
cout<<Q.GQ()<<endl;
}
链栈
最新推荐文章于 2022-08-17 11:17:33 发布