#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -2
#define OVERFLOW -1
#define MAXSIZE 6//最大队列长度
#include
#include
using namespace std;
typedef int Status; //Status
相当于 int
typedef struct SeQueue
{
int *
base; //初始化的动态分配空间
int
rear; //尾指针
int front; //头指针
}SeQueue;
//***********************函数定义*****************************
void InitSeQueue(SeQueue
&Q) //初始化一个空队列
{
Q.base = (int *)malloc(sizeof(int) *
MAXSIZE);
if(!Q.base )
exit(OVERFLOW); //存储分配失败
Q.front = Q.rear = 0;
cout<<"初始化成功!"<<endl;
return ;
}
Status SeQueueEmpty(SeQueue Q)//判断是否为空
{
if(Q.front == Q.rear)
{
cout<<"队列为空!"<<endl;
return TRUE;
}
else return FALSE;
}
Status SeQueueFull(SeQueue Q)//判断是否为空
{
if((Q.rear +1)%MAXSIZE == Q.front )
{
cout<<"队列已满!"<<endl;
return TRUE;
}
else return FALSE;
}
void TraverseSeQueue(SeQueue Q)//输出当前队列,从对头到队尾
{
int p = Q.front ;
cout<<"队列中的元素为:";
while( p != Q.rear )
{
cout<<Q.base[p]<<"
";
p = (p + 1)%MAXSIZE;
}
cout<<endl;
}
void EnQueue(SeQueue &Q,int e )//插入元素e为新的队头元素
{
if(SeQueueFull(Q) ) return
;
Q.base[Q.rear]= e;
Q.rear = (Q.rear + 1) % MAXSIZE;
return;
}
void DeQueue(SeQueue &Q,int &e)//出队,出队列元素为e
{
if(SeQueueEmpty(Q)) return ;
e = Q.base[Q.front];
Q.front = (Q.front +1) % MAXSIZE ; return
;
}
void SeQueueLength(SeQueue Q)//返回队列的长度
{
cout<<"队列的长度为:" <<(Q.rear - Q.front +
MAXSIZE) % MAXSIZE <<endl;
}
void Getfront(SeQueue Q)
{
if(SeQueueEmpty(Q)) return;
cout<<"队头元素为:"<<
Q.base[Q.front]<<endl;
}
void Getrear(SeQueue Q)
{
if(SeQueueEmpty(Q)) return;
cout<<"队尾元素为:"<<
Q.base[Q.rear-1]<<endl;
}
//***********************主函数*****************************
int main(void)
{
SeQueue Q;//定义一个队列
int door = 1,init = 0,num;
while( door )
{
printf("\n*****************************************************************\n");
printf("
如要对队列进行操作,请输入各项前面相应的数字\n");
printf("
************************************\n");
printf("
|
1、初始化队列
|\n");
printf("
|
2、入队
|\n");
printf("
|
3、出队
|\n");
printf("
|
4、查看当前队列
|\n");
printf("
|
5、查看队列长度
|\n");
printf("
|
6、查看队头的元素
|\n");
printf("
|
7、查看队尾的元素
|\n");
printf("
|