#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define MAX_QSIZE 30
typedef int Status;
typedef int QElemType;
typedef struct
{ QElemType *base;
int front;
int rear;
}SqQueue;
//初始化循环队列
int InitQueue(SqQueue &Q)
{ /* 构造一个空队列Q */
Q.base=(QElemType*)malloc(sizeof(QElemType)*MAX_QSIZE);
if(!Q.base) return ERROR;
Q.front=Q.rear=0;
return OK;
}
//判断队空
//入队
Status EnQueue(SqQueue &Q,QElemType e)
{ /* 插入元素e为Q的新的队尾元素 */
if((Q.rear+1)%MAX_QSIZE==Q.front) return OVERFLOW;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAX_QSIZE;
return OK;
}
//出队
Status DeQueue(SqQueue &Q,QElemType &e)
{ /* 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR */
if(Q.rear==Q.front) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAX_QSIZE;
return OK;
}
void Print(SqQueue Q)
{
int i;
for(i=Q.front;i<Q.rear;i++)
printf("%4d",Q.base[i]);
printf("\n");
}
int main()
{
SqQueue qu;
QElemType x;
InitQueue(qu);
EnQueue(qu,10);
EnQueue(qu,20);
Print(qu);
DeQueue(qu,x);
Print(qu);
return 0;
}
循环队列
最新推荐文章于 2023-12-03 11:39:34 发布