链队

//链队
#include<iostream.h>
template<class T>
struct Node
{
	T data;
	Node<T> *next;
};
template<class T>
class C
{
	public:
		C();
		~C();
		void En(T x);
		T De();
		T Get();
		int Empty();
	private:
		Node<T> *front,*rear;
};
template<class T>
C<T>::C()
{
	Node<T> *s=NULL;
	s=new Node<T>;
	s->next=NULL;
	front=rear=s;
}
template<class T>
C<T>::~C()
{
	Node<T> *p=NULL;
	while(front!=NULL)
	{
		p=front->next;
		delete front;
		front=p;
	}
}
template<class T>
void C<T>::En(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 C<T>::De()
{
	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 C<T>::Get()
{
	if(front!=rear)
		return front->next->data;
}
template<class T>
int C<T>::Empty()
{
	if(front==rear)
		return 1;
	else
		return 0;
}
void hehe()
{
	cout<<"1.入队操作"<<endl;
	cout<<"2.出队操作"<<endl;
	cout<<"3.查看队头"<<endl;
	cout<<"4.推出程序"<<endl;
}
int main()
{
	int i;
	C<int> s;
	if(s.Empty())
		cout<<"队列为空"<<endl;
	else
		cout<<"队列非空"<<endl;
	do{
    hehe();
	cin>>i;
switch(i)
{
case 1:int a;cout<<"请输入元素:";cin>>a; s.En(a);break;
case 2:cout<<"已出队一个元素"<<endl; s.De();break;
case 3:cout<<"队头元素:"<<endl;cout<<s.Get()<<endl;break;
case 4:i=0;break;
default:cout<<endl;break;
	}}
while(i!=0);
	return 0;
	}

猜你喜欢

转载自blog.csdn.net/weixin_41936498/article/details/80086206