代码:
#include<iostream>
using namespace std;
template<class T>
class sq_Queue
{private:
int mm;
int front;
int rear;
int s;
T *q;
public:
sq_Queue(int);
void prt_sq_Queue();
int flag_sq_Queue();
void ins_sq_Queue(T);
T del_sq_Queue();
};
template<class T>
sq_Queue<T>::sq_Queue(int m)
{
mm=m;
q=new T[mm];
front=mm;rear=mm;s=0;
return;
}
template<class T>
void sq_Queue<T>::prt_sq_Queue()
{
int i;
cout<<"front="<<front<<endl;
cout<<"rear="<<rear<<endl;
if(s==0){cout<<"队列空!"<<endl;return;}
i=front;
do
{
i=i+1;
if(i==mm+1)i=1;
cout<<q[i-1]<<endl;
}while(i!=rear);
return;
}
template<class T>
int sq_Queue<T>::flag_sq_Queue()
{
if((s==1)&&(rear==fornt))return(-1);
if(s==0)return 0;
return(1);
}
template<class T>
void sq_Queue<T>::ins_sq_Queue(T x)
{
if((s==1)&&(rear==front))
{cout<<"Queue_overflow!"<<endl;return;}
rear=rear+1;
if(rear==mm+1)rear=1;
q[rear-1]=x;
s=1;
return;
}
template<class T>
T sq_Queue<T>::del_sq_Queue()
{
T y;
if(s==0)
{cout<<"Queue_underflow!"<<endl;return(0);}
front=front+1;
if(front==mm+1)front=1;
y=q[front-1];
if(front==rear)s=0;
return(y);
}
int main()
{
sq_Queue<int> q(10);
cout<<"输出排头与队尾指针以及队中元素:"<<endl;
q.prt_sq_Queue();
q.ins_sq_Queue(50);
q.ins_sq_Queue(60);
q.ins_sq_Queue(70);
q.ins_sq_Queue(80);
q.ins_sq_Queue(90);
q.ins_sq_Queue(100);
cout<<"输出排头与队尾指针以及队中元素:"<<endl;
q.prt_sq_Queue();
cout<<"输出退队元素:"<<endl;
cout<<q.del_sq_Queue()<<endl;
cout<<q.del_sq_Queue()<<endl;
cout<<q.del_sq_Queue()<<endl;
cout<<"输出排头与队尾指针以及队中元素:"<<endl;
q.prt_sq_Queue();
return 0;
}
运行效果: