循环队列的几个基本操作
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct
{
int *base;
int front;
int rear;
}sqQueue;
bool Init(sqQueue &Q)
{
Q.base = new int[MAXSIZE];
if(!Q.base)
exit;
Q.front=Q.rear=0;
return true;
}
int QueueLength(sqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
bool EnQueue(sqQueue &Q,int e)
{
if((Q.rear+1)%MAXSIZE == Q.front)
return false;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1)%MAXSIZE;
return true;
}
bool DeQueue(sqQueue &Q,int &e)
{
if(Q.rear == Q.front)
return false;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return true;
}
int GetHead(sqQueue Q)
{
if(Q.rear != Q.front)
return Q.base[Q.front];
}
int main ()
{
int i,e;
sqQueue Q;
Init(Q);
EnQueue(Q,5);
EnQueue(Q,4);
EnQueue(Q,3);
DeQueue(Q,e);
cout<<e<<endl;
i=QueueLength(Q);
cout<<i<<endl;
return 0;
}