循环队列

#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("             0、退出                      |\n");
  printf("          ************************************\n");
  cout<<" 请输入数字进行操作:";
  cin>>num;
  if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7)
  {
   switch(num)
   {
    case 0: door = 0; break;
    case 1: InitSeQueue(Q); init = 1; break;
    case 2:
     if(init){  int e; cout<<"请输入要入队列的元素:";  cin>>e; EnQueue(Q,e); break;}
     cout<<"请先对队列进行初始化!"<<endl;break;
    case 3: 
     if(init){  int e; DeQueue(Q,e); cout<<"出队列成功!出队列元素为:"<<e<<endl; break;}
     cout<<" 请先对队列进行初始化!"<<endl;break;
    case 4: 
     if(init){ TraverseSeQueue(Q); break; }
     cout<<" 请先对队列进行初始化!"<<endl;break;
    case 5: 
     if(init){ SeQueueLength(Q); break;}
     cout<<" 请先对队列进行初始化!"<<endl;break;
    case 6: 
     if(init){ Getfront(Q); break;}
     cout<<" 请先对队列进行初始化!"<<endl;break;
    case 7: 
     if(init){ Getrear(Q); break;}
     cout<<" 请先对队列进行初始化!"<<endl;break;
   }
  }
  else
  cout<<"输入不正确请重新输入!"<<endl; continue; }
 }
 return 0;
}

/*
1