}/*main{}*/
循环队列
队列的定义:
(文件名称ds.h)
#include
usingnamespacestd;
typedefintElemType;
#defineMAXSIZE10
typedefstructSqQueue{
ElemTypedata[MAXSIZE];
intfront;
intrear;
}SqQueue;
voidInitQueue(SqQueue*Q);//初始化循环队列
intQueueEmpty(SqQueue*Q);//判断是否为空
intQueueFull(SqQueue*Q);//判断是否已满
voidEnQueue(SqQueue*Q,ElemTypee);//入队
ElemTypeDeQueue(SqQueue*Q);//出队
voidOutQueue(SqQueueQ);//遍历
//intGetQueue(SqQueue*Q);//对头函数
函数的具体含义:
(文件的名称ds.cpp)
#include
usingnamespacestd;
typedefintElemType;
#defineMAXSIZE10
typedefstructSqQueue
{
ElemTypedata[MAXSIZE];
intfront;
intrear;
}SqQueue;
//初始化循环队列
voidInitQueue(SqQueue*Q){
Q->front=0;
Q->rear=0;
}
//判断循环队列是否为空
intQueueEmpty(SqQueue*Q){
if(Q->front==Q->rear)
return1;
else
return0;
}
//判断循环队列是否已满
intQueueFull(SqQueue*Q){
if((Q->rear+1)%MAXSIZE==Q->front)
return1;
else
return0;
}
//入队
voidEnQueue(SqQueue*Q,ElemTypee){
if(QueueFull(Q)){cout<<"此队列已满,无法进行入队操作。
";return;}
else
{Q->data[Q->rear]=e;//赋值
Q->rear=(Q->rear+1)%MAXSIZE;//修改尾指针
return;
}
}
//出队
ElemTypeDeQueue(SqQueue*Q){
ElemTypee;
if(QueueEmpty(Q)){cout<<"此队列已空,无法进行出队操作。
";return0;}//判断是否为空
else
{e=Q->data[Q->front];//赋值给e
Q->front=(Q->front+1)%MAXSIZE;//修改头指针
return(e);}
}
//遍历队列
voidOutQueue(SqQueueQ){
{
inti;
if(QueueEmpty(&Q))
cout<<"该表为空表,无法再进行遍历操作。
";//判断是否为满
for(i=Q.front;icout<}
}
//出对头元素
/*intGetQueue(SqQueue*Q){
if(QueueEmpty(Q)){cout<<"此队列已空,无法进行出队操作。
";return0;}
else
return(Q->data[Q->rear-1]);
}*/
主函数(文件名称dsmain.cpp)
#include
#include"ds.h"
usingnamespacestd;
voidmain(){
SqQueueQ;//定义一个Q
intk;
do
{
cout<<"\n";
cout<<"\n";
cout<<"\n1.初始化队列";
cout<<"\n2.入队";
cout<<"\n3.出队";
//cout<<"\n4.取对头元素";
cout<<"\n5.退出程序";
cout<<"\n======================================";
cout<<"\n请输入您的选择命令(1,2,3,4)";
cin>>k;cout<<"\n";
switch(k)
{
case1:
{
InitQueue(&Q);
cout<<"\nok"<case2:
{
inte;
cout<<"请输入e的值:
";
cin>>e;
EnQueue(&Q,e);
cout<<"\n此时的循环队列为:
";
OutQueue(Q);//输出现在队列中的数
cout<cout<<"\nok"<case3:
{
inte;
e=DeQueue(&Q);
if(e!
=0)
cout<cout<<"\n此时的循环队列为:
";
OutQueue(Q);//输出现在队列中的数
cout</*case4:
{
inttemp=GetQueue(&Q);
cout<<"对头元素:
"<cout<<"\nok"<}
}while(k!
=4);/*switch{}*/
cout<<"\n再见!
";
cout<<"\n打回车键,返回。
"<}/*main{}*/