顺序循环队列的基本操作(二)

/*增设tag数据,区分队满队空*/
#include<stdio.h>
#define MaxSize 10
typedef char ElemType;
typedef struct 
{
	ElemType data[MaxSize];
	int front,rear,tag;
}SqQueue;
bool InitQueue(SqQueue &q)
{
	q.front=q.rear=0;//初始化队头队尾指针
	q.tag=0;
	return true;
}
bool EmptyQueue(SqQueue q)
{
	if(q.rear==q.front && q.tag==0)		//队空的条件:首尾指针指向同一地址且标记tag为0
		return true;
	return false;
}
bool EnQueue(SqQueue &q,ElemType e)
{
	if(q.rear==q.front && q.tag==1)		//队满的条件(首尾指针指向同一地址且标记tag为1)
		return false;
	q.data[q.rear++]=e;
	q.tag=1;
	return true;
}
bool OutQueue(SqQueue &q,ElemType &e)
{
	if(q.rear==q.front && q.tag==0)		//判空
		return false;
	e=q.data[q.front++];
	q.tag=0;
	return true;
}
bool HeadQueue(SqQueue q,ElemType &e)
{
	if(q.rear==q.front && q.tag==0)
		return false;
	e=q.data[q.front];
	return true;
}
void main()
{
	SqQueue q;
	InitQueue(q);
	ElemType e;
	printf("The Queue is %s\n",(EmptyQueue(q)?"Empty!":"UnEmpty!"));
	EnQueue(q,'a');
	EnQueue(q,'b');
	EnQueue(q,'c');
	HeadQueue(q,e);
	printf("Queue_Head=%c\n",e);
	OutQueue(q,e);
	HeadQueue(q,e);
	printf("Queue_Head=%c\n",e);
}

  

猜你喜欢

转载自www.cnblogs.com/-slz-2/p/13199359.html