循环队列的长度、入队、出队和取队头元素

一、队列的定义:

#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100 //队列可能达到的最大长度 
typedef int QElemType;
typedef int Status;
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef struct
{
	QElemType *base;//存储空间的基地址; 
	int front;//头指针 
	int raer;//尾指针 
}SqQueue;//循环队列;

二、初始化:

①为队列分配一个容量为MAXQSIZE的数组空间,base指向首地址;

②将头指针和尾指针置为0,表队空;

Status InitQueue(SqQueue &Q)
{//构造一个空队列Q; 
	Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间; 
	if(!Q.base) exit(OVERFLOW);//存储分配失败 
	Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空; 
	return OK;
}

三、求队列长度:

①将头指针减去尾指针加上最大数组空间然后进行取余

void QueueLength(SqQueue Q)
{//返回Q的元素个数,即队列的长度; 
	int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
	printf("队列长度为:%d\n",x);
}

四、入队:

①判断队列是否满,若满则返回ERROR;

②将新元素插入队尾;

③队尾元素+1;

Status EnQueue(SqQueue &Q,QElemType e)
{//插入元素为e位Q的新的队尾元素 
	if((!Q.raer+1)%MAXQSIZE==Q.front)
	//若尾指针在循环意义上加1后等于头指针,表明队满 
	return ERROR;
	Q.base[Q.raer]=e;//新元素插入队尾 
	Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1 
	return OK;
}

五、出队:

①判断队列是否为空,若空则返回ERROR;

②保存队头元素;

③队头指针+1;

Status DeQueue(SqQueue &Q,QElemType &e)
{//删除Q的队头元素,用 e返回其值 
	if(Q.front==Q.raer) return ERROR;//队空 ; 
	e=Q.base[Q.front];//保存队头元素 ; 
	printf("将元素%d出队\n",e);
	Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1; 
	return OK;
}

六、取队头元素:

①Q.base为队头序号,直接利用Q.base输出;

QElemType GetHead(SqQueue Q)
{//返回Q的队头元素,不修改队头指针 
	printf("队头元素为:"); 
	if(Q.front!=Q.raer)    //队列非空 
	    return Q.base[Q.front];//返回 
}

七、主函数:

int main()
{
	SqQueue Q;
	int x,e;
	InitQueue(Q);
	EnQueue(Q,0);
	EnQueue(Q,1);
	EnQueue(Q,2);
	EnQueue(Q,3);
	QueueLength(Q);
	DeQueue(Q,e);
	DeQueue(Q,e);
	QueueLength(Q);
	int k=GetHead(Q) ;
	printf("%d",k);
	return 0;
	
}

八、完整代码:

#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100 //队列可能达到的最大长度 
typedef int QElemType;
typedef int Status;
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef struct
{
	QElemType *base;//存储空间的基地址; 
	int front;//头指针 
	int raer;//尾指针 
}SqQueue;//循环队列; 
Status InitQueue(SqQueue &Q)
{//构造一个空队列Q; 
	Q.base=new QElemType[MAXQSIZE];//为队列分配一个最大容量位MAXQSIZE的数组空间; 
	if(!Q.base) exit(OVERFLOW);//存储分配失败 
	Q.front=Q.raer=0;//将头指针和尾指针置为0,队列为空; 
	return OK;
}
void QueueLength(SqQueue Q)
{//返回Q的元素个数,即队列的长度; 
	int x=(Q.raer-Q.front+MAXQSIZE)%MAXQSIZE;
	printf("队列长度为:%d\n",x);
}
Status EnQueue(SqQueue &Q,QElemType e)
{//插入元素为e位Q的新的队尾元素 
	if((!Q.raer+1)%MAXQSIZE==Q.front)
	//若尾指针在循环意义上加1后等于头指针,表明队满 
	return ERROR;
	Q.base[Q.raer]=e;//新元素插入队尾 
	Q.raer=(Q.raer+1)%MAXQSIZE;//队尾指针加1 
	return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e)
{//删除Q的队头元素,用 e返回其值 
	if(Q.front==Q.raer) return ERROR;//队空 ; 
	e=Q.base[Q.front];//保存队头元素 ; 
	printf("将元素%d出队\n",e);
	Q.front=(Q.front+1)%MAXQSIZE;//队头指针加1; 
	return OK;
}
QElemType GetHead(SqQueue Q)
{//返回Q的队头元素,不修改队头指针 
	printf("队头元素为:"); 
	if(Q.front!=Q.raer)    //队列非空 
	    return Q.base[Q.front];//返回 
}
int main()
{
	SqQueue Q;
	int x,e;
	InitQueue(Q);
	EnQueue(Q,0);
	EnQueue(Q,1);
	EnQueue(Q,2);
	EnQueue(Q,3);
	QueueLength(Q);
	DeQueue(Q,e);
	DeQueue(Q,e);
	QueueLength(Q);
	int k=GetHead(Q) ;
	printf("%d",k);
	return 0;
	
}

九、运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值