代码如下
#include<bits/stdc++.h>
#define MAXQSIZE 100
#define ERROR 0
#define OK 1
using namespace std;
//顺序队列结构体的基本定义
typedef int QElemType;
typedef int Status;
typedef struct Queue
{
QElemType *base;//初始化的动态分配存储空间
int front;//头指针,若队列不为空,则指向队头元素
int rear;//尾指针,若队列不为空,则指向队尾元素的下一个位置
}SqQueue;
//循环队列的初始化
Status InitQueue(SqQueue &q)
{
q.base = new QElemType[MAXQSIZE];//分配数组空间
if(!q.base) return ERROR;//分配失败
q.front = q.rear = 0;//分配成功时,头指针尾指针置0,队列为空
return OK;
}
//循环队列的判空
bool QueueEmpty(SqQueue q)
{
if(q.front == q.rear)
return true;
else
return false;
}
//循环队列的判满
bool QueueFull(SqQueue q)
{
if((q.rear+1)%MAXQSIZE==q.front)
return true;
else
return false;
}
//循环队列的清空
Status ClearQueue(SqQueue &q)
{
q.front = q.rear =0;
return OK;
}
//求队列长度
int QueueLength(SqQueue q)
{
return ((q.rear - q.front + MAXQSIZE) % MAXQSIZE);
}
//循环队列入队
Status InQueue(SqQueue &q,QElemType e)
{
if((q.rear+1)%MAXQSIZE==q.front) return ERROR;//循环队列的判满
q.base[q.rear]=e;//新元素加入到队尾
q.rear = (q.rear+1)%MAXQSIZE;//队尾指针+1
return OK;
}
//循环队列的出队
Status OutQueue(SqQueue &q,QElemType &e)
{
if(q.front==q.rear) return ERROR; //队空
e = q.base[q.front];//用e保存队头元素
q.front=(q.front+1)%MAXQSIZE;//队头指针+1
return OK;
}
//取队头元素
int GetQueue(SqQueue q)
{
if(q.front == q.rear) return ERROR;//进行判空操作
int k;
k = q.base[q.front];
cout << "队头元素为:"<<k<<endl;
return OK;
}
//遍历打印
Status QueuePrint(SqQueue q)
{
int i;
i = q.front;
while(i!=q.rear)
{
cout <<q.base[i] <<" ";
i = (i+1)%MAXQSIZE;
}
cout <<endl;
return OK;
}
//*************************************************************
//******************功能函数编写********************************
//*************************************************************
//获取长度
void GetLength(SqQueue q)
{
int k;
k = QueueLength(q);
cout << "当前队列长度为:"<<k<<endl;
}
//判满
void IsFull(SqQueue q)
{
bool k;
k = QueueFull(q);
if(k==true)
{
cout << "队列已满!"<<endl;
}
else
{
cout << "队列未满"<<endl;
}
}
//清空
void Empty(SqQueue &q)
{
int k;
k = ClearQueue(q);
if(k)
{
cout <<"队列已清空!"<<endl;
}
}
//判空
void IsEmpty(SqQueue q)
{
bool k;
k = QueueEmpty(q);
if( k == true)
{
cout<<"当前队列为空!"<<endl;
}
else
{
cout <<"不为空!"<<endl;
}
}
//元素入队
void IN(SqQueue &q)
{
int k;
QElemType e;
cout << "请输入你要入队的元素的值"<<endl;
cin >> e;
k = InQueue(q,e);
if(k)
{
cout <<"元素已成功入队"<<endl;
QueuePrint(q);
}
else
{
cout <<"入队失败!队已满"<<endl;
}
}
void OUT(SqQueue &q)
{
int k;
QElemType e;
k = OutQueue(q,e);
if(k)
{
cout << "出队成功!"<<endl;
QueuePrint(q);
}
else
{
cout <<"出队失败!队已空"<<endl;
}
}
void menu()
{
cout << "1.打印"<<endl;
cout << "2.长度"<<endl;
cout << "3.判满"<<endl;
cout << "4.清空"<<endl;
cout << "5.判空"<<endl;
cout << "6.入队"<<endl;
cout << "7.出队"<<endl;
cout << "8.取队头元素"<<endl;
cout << "9.退出"<<endl;
}
int main()
{
int n;
SqQueue q;
cout << "请输入你要输入数字的个数"<<endl;
cin >> n;
cout <<"请输入数字"<<endl;
int j,a[n];
j = InitQueue(q);
if(j==1)
{
for(int i = 0;i<n;i++)
{
cin >> a[i];
InQueue(q,a[i]);
}
}
QueuePrint(q);
int ch;
while (1)
{
menu();
printf("请输入菜单序号:\n");
cin >> ch;
if (9 == ch) break;
switch (ch)
{
case 1:QueuePrint(q); break;
case 2:GetLength(q); break;
case 3:IsFull(q); break;
case 4:Empty(q);break;
case 5:IsEmpty(q); break;
case 6:IN(q); break;
case 7:OUT(q); break;
case 8:GetQueue(q); break;
default:printf("输入错误!!!\n");
}
}
return 0;
}