#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define MAXQSIZE 100
typedef int Status;
typedef int QElemType;
typedef struct
{
int *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue *Q)
{
Q->base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q->base) return ERROR;
Q->front=Q->rear=0;
return 0;
}
Status QueueLength(SqQueue *Q)
{
int length;
length=(Q->rear-Q->front+MAXQSIZE)%MAXQSIZE;
return length;
}
Status EnQueue(SqQueue *Q,QElemType e)
{
if((Q->rear+1)%MAXQSIZE==Q->front) return ERROR;
Q->base[(Q->rear+1)]=e;
Q->rear=(Q->rear+1)%MAXQSIZE;
return 0;
}
Status DeQueue(SqQueue *Q)
{
int e,test;
if(Q->front==Q->rear) return ERROR;
test=Q->front;
e=Q->base[Q->front];
Q->front=(Q->front+1) % MAXQSIZE;
return e;
}
void JudgeEmpty(SqQueue *Q)
{
if((Q->rear+1)%MAXQSIZE==Q->front) printf("The queue is full\n");
else if(Q->rear==Q->front) printf("The queue is Empty\n");
else printf("The queue is not empty!\n");
}
/*
void DestroyQueue(SqQueue *Q)
{
}
*/
int main()
{
SqQueue Q;
int number,receive,i,transmission;
int length;
InitQueue(&Q);
printf("how many numbers do you want to enter:");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
printf("The %d number:",i);
scanf("%d",&transmission);
EnQueue(&Q,transmission );
}
JudgeEmpty(&Q);
length=QueueLength(&Q);
printf("the length is %d\n",length);
receive=DeQueue(&Q);
printf("The first number is:%d",receive);
printf("\n");
}
出队函数那个调试有问题,设置了一个test来检验,但是好像都不对,但是我觉得我没敲错啊
慕设计2395807
相关分类