循环队列基本操作

/***循环队列基本操作***/
#include<iostream><img src="https://img-blog.youkuaiyun.com/20141024134125207?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc5NTc2Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
using namespace std;

#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct{
	QElemType *base;
	int front;
	int rear;
}SqQueue;
//初始化
Status InitQueue(SqQueue &Q)
{
	Q.base=new QElemType[MAXQSIZE];
	if(!Q.base) return OVERFLOW;
	Q.front=Q.rear=0;
	return OK;
} 
//求队列长度
int QueueLength(SqQueue Q)
{
	return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
} 
//入队 
Status EnQueue(SqQueue &Q,QElemType e)
{
	if((Q.rear+1)%MAXQSIZE==Q.front)
	    return ERROR;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE;
	return OK; 
}
//出队
Status DeQueue(SqQueue &Q,QElemType &e)
{
	if(Q.front==Q.rear)
	    return ERROR;
    e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXQSIZE;
    return OK;
}
int main()
{
	SqQueue q;
	QElemType e;
	cout<<"进循环队列的元素依次为:"<<endl;
	if(InitQueue(q))
	{
		for(int i=1;i<=12;i++)
		{
			EnQueue(q,i);
			cout<<i<<" ";
		} 
	}
	cout<<endl;
	cout<<"队列的长度为:\n";
	cout<<QueueLength(q)<<endl;
    cout<<"出循环队列的元素依次为:"<<endl;
    while(DeQueue(q,e))
    {   
    	cout<<e<<" ";
    }
    cout<<endl;
    return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值