数据结构之循环队列的实现

/********************循环队列(顺序存储结构)******************/

#include"stdio.h"
#include"stdlib.h"
#define MAXSIZE 20
#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
typedef int ElemType;
typedef int Status;
typedef struct
{
	ElemType data[MAXSIZE];
	int front;
	int rear;
}SqQueue;


/****************Operator***********************/


void InitQueue(SqQueue *Q)				//初始化队列
{
	Q->front=Q->rear=0;
}


void ClearQueue(SqQueue *Q)				//清空队列
{
	Q->front=Q->rear;
}


void DestroyQueue(SqQueue *Q)			//销毁队列
{
	free(Q);
}


Status EmptyQueue(SqQueue Q)			//判断队列是否为空
{
	if(Q.front==Q.rear)
		return TRUE;
	return FALSE;
}


int QueueLength(SqQueue Q)				//返回队列的长度
{
	return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}


Status EnQueue(SqQueue *Q, ElemType e)	//入队操作
{
	if((Q->rear+1)%MAXSIZE==Q->front)	//判断队列是否满了
		return ERROR;
	Q->data[Q->rear]=e;
	Q->rear=(Q->rear+1)%MAXSIZE;
	return OK;
}


Status DeQueue(SqQueue *Q,ElemType *e)	//出队操作,将队头元素返回给e
{
	if(EmptyQueue(*Q))
		return ERROR;
	*e=Q->data[Q->front];
	Q->front=(Q->front+1)%MAXSIZE;
	return OK;
}


void PrintQueue(SqQueue Q)				//打印队列
{
	int i;
	if(!EmptyQueue(Q))
	{
		for(i=Q.front;i<Q.rear;i++)
		{
			printf("%6d",Q.data[i]);
		}
		printf("\n");

	}
}
main(void)
{
	SqQueue *Q;
	ElemType *e;
	e=(ElemType*)malloc(sizeof(ElemType));
	Q=(SqQueue *)malloc(MAXSIZE * sizeof(SqQueue));
	InitQueue(Q);
	EnQueue(Q,12);
	EnQueue(Q,14);
	EnQueue(Q,13);
	PrintQueue(*Q);
	DeQueue(Q,e);
	PrintQueue(*Q);
	printf("%6d\n",*e);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值