データ構造-循環キューの基本的な実現操作
1484 ワード
/*main*/
#include
#include
#define TRUE 1
#define ERROR 0
#define FALSE 0
#define OK 1
#define OVERFLOW -2
#define MAXSIZE 20
typedef int Status;
typedef int QElemtype;
typedef struct{
QElemtype *base;
int front;
int rear;
}SqQueue;
#include"xhdl.h"
main()
{
SqQueue Q;
Init(Q);
int cnt=1,k;
while(cnt<11)
{
EnQueue(Q,cnt);
cnt++;
}
printf(" :");
print(Q);
putchar(10);
printf(" :%d",QueueLength(Q));
putchar(10);
printf(" :");
for(int i=0;i<2;i++)
printf("%d ",DeQueue(Q,k));
putchar(10);
printf(" :");
print(Q);
putchar(10);
printf(" :%d",QueueLength(Q));
return OK;
}
/*xhdl*/
Status Init(SqQueue &Q)
{
Q.base=(int *)malloc(MAXSIZE*sizeof(int));
if(!Q.base)exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status EnQueue(SqQueue &Q,QElemtype e)
{
if((Q.rear+1)%MAXSIZE==Q.front)return ERROR;// ;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
Status QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
Status DeQueue(SqQueue &Q,int &e)
{
if(Q.front==Q.rear)return ERROR;// ;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return e;
}
Status print(SqQueue &Q)
{
if(Q.front==Q.rear)
{
printf(" ");
return ERROR;
}
int i=Q.front;
if(Q.front