#include<iostream>
#define MAXSIZE 6
using namespace std;
typedef struct
{
int *base;
int rear;
int front;
}Queue;
void InitQueue(Queue &Q)
{
Q.base=new int[MAXSIZE];
Q.front=0;
Q.rear=0;
return;
}
int lengthQueue(Queue Q)
{
return(Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
bool InQueue(Queue &Q,float e)
{
if((Q.rear+1)%MAXSIZE==Q.front)
return false;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return true;
}
int OutQueue(Queue &Q)
{
float a;
if(Q.front==Q.rear)
return 0;
a=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return a;
}
int GetQueue(Queue Q)
{
if(Q.rear!=Q.front)
return Q.base[Q.front];
}
void output(Queue Q)
{
if(!Q.base)
{
cout<<"队列为空,无法输出!"<<endl;
}
int i=(Q.rear-Q.front+MAXSIZE)%MAXSIZE;
for(i;i>0;i--)
{
cout<<Q.base[Q.front]<<" ";
Q.front=(Q.front+1)%MAXSIZE;
}
cout<<endl;
return;
}
int main()
{
Queue Q;
while(1)
{
cout<<"*********************************************************"<<endl;
cout<<"******1.顺序队列初始化 2.顺序队列数据填充********"<<endl;
cout<<"******3.顺序队列的长度 4.顺序队列的入队**********"<<endl;
cout<<"******5.顺序队列的出队 6.顺序队列的对头元素******"<<endl;
cout<<"******7.顺序队列的显示 8.退出程序 ************"<<endl;
int k;
int n;
float e;
int a=1;
cout<<"输入你的选择:";
cin>>k;
switch(k)
{
case 1:
InitQueue(Q);
cout<<"顺序队列初始化成功!"<<endl;
continue;
case 2:
cout<<"输入你要填充的元素的个数:";
cin>>n;
for(a;a<=n;a++)
{
cout<<"输入入队的第"<<a<<"个数:";
cin>>e;
InQueue(Q,e);
}
cout<<"数据输入成功!"<<endl;
continue;
case 3:
cout<<"顺序队列的长度为:";
cout<<lengthQueue(Q)<<endl;
continue;
case 4:
cout<<"输入你要入队的元素:";
cin>>e;
InQueue(Q,e);
cout<<"元素入队成功!"<<endl;
continue;
case 5:
cout<<"循环队列出队的的元素值为:";
cout<<OutQueue(Q)<<endl;
cout<<"元素出队成功!"<<endl;
continue;
case 6:
cout<<"循环队列的对头元素为:";
cout<<GetQueue(Q)<<endl;
continue;
case 7:
cout<<"顺序队列的显示如下:"<<endl;
output(Q);
cout<<"显示完毕!"<<endl;
continue;
case 8:
cout<<"退出程序!"<<endl;
break;
}
break;
}
return 0;
}
该代码用C++进行编译,实现创建顺序队列及队列中相关操作。
|