顺序队(初始化,入队,出队,取队首元素)

顺序队数组中数量有限为增加利用率,我们采用循环队。

即当入队时将队尾序号由(real+1)改为(real+1)%MAXSIZE

当出队时队头序号由(front+1)改为(front+1)%MAXSIZE

一.结构的定义

typedef struct
{
	ElemType *elem;//指针 
	int front;//对头序号 
	int real;//队尾序号 
}sqQueue;

二.初始化

1.创建数组指针指向首地址。

2.初始化头尾序号为0(当输入一个数据队尾+1)。

代码:

status InitQueue(sqQueue &Q)
{
	Q.elem=new ElemType[MAXSIZE];
	if(!Q.elem) return ERROR;
	Q.front=Q.real=0;
	return OK;
} 

三.入队

1.首先判断是否队满((Q.real+1)%MAXSIZE==Q.front)。

2.将数据存入数组(Q.elem[Q.real]=e)。

3.队尾序号++。

代码:

status push(sqQueue &Q,ElemType e)
{
	if((Q.real+1)%MAXSIZE==Q.front) return ERROR;
	Q.elem[Q.real]=e;
	Q.real=(Q.real+1)%MAXSIZE;
	return OK;
}

四.出队

1.判断是否队空(Q.front==Q.real则队空)。

2.将队首元素赋给e。

3.队头序号++。

代码:

status pop(sqQueue &Q,ElemType e)
{
	if(Q.front==Q.real) return ERROR;
	e=Q.elem[Q.front];
	Q.front=(Q.front+1)%MAXSIZE;
	return OK;
}

五.提取队首元素

代码:

status Get(sqQueue &Q,ElemType e)
{
	if(Q.front==Q.real) return ERROR;
	e=Q.elem[Q.front];
	return OK;
}

六.展示

1.定义i等于front,不断输出第i个元素,i不断向上移动,直到i等于real(条件)

代码:

status show(sqQueue &Q)
{
	int i=Q.front;
	if(Q.front==Q.real)  cout<<"队列为空"<<endl;
	while(i!=Q.real)
	{
		cout<<Q.elem[i]<<" ";
		i=(i+1)%MAXSIZE;
	}
	return OK;
}

总代码:

#include<iostream>
#include<stdlib.h>
#define OK 1
#define ERROR 0
using namespace std;
typedef int ElemType; 
typedef int status;
#define MAXSIZE 100
typedef struct
{
	ElemType *elem;//指针 
	int front;//对头序号 
	int real;//队尾序号 
}sqQueue;
status InitQueue(sqQueue &Q);
status push(sqQueue &Q,ElemType e);
status pop(sqQueue &Q,ElemType e);
status Get(sqQueue &Q,ElemType e);
status show(sqQueue &Q); 
int main()
{
	int choice;
	sqQueue Q;
	while(1)
	{
		cout<<"1.初始化  2.入队   3.出队   4.取对头元素  5.展示"<<endl;
		cout<<"6.求队列长度   7.判断队空   8.判断队满"<<endl;
		cout<<"请输入选项:";
		cin>>choice;
		switch(choice)
		{
			case 1:
				if(InitQueue(Q)==ERROR) cout<<"初始化失败"<<endl;
				else cout<<"初始化成功"<<endl;
				break;
			case 2:
				ElemType e;
				cout<<"入队数据:";
				cin>>e;
				if(push(Q,e)==ERROR) cout<<"队满"<<endl;
				else cout<<"入队成功"<<e<<endl;
				break;	
			case 3:
			{	
				ElemType e;
				if(pop(Q,e)==ERROR) cout<<"队空"<<endl;
				else cout<<"出队成功"<<e<<endl;
				break;
			}
			case 4:
			{		
				ElemType e;
				if(Get(Q,e)==ERROR) cout<<"队空"<<endl;
				else cout<<e;
				break;
			}
			case 5:
				if(show(Q)==ERROR)  cout<<"队列为空"<<endl;
				break; 
			case 6:	
		}
		system("pause");
		system("cls");
	}
}
status InitQueue(sqQueue &Q)
{
	Q.elem=new ElemType[MAXSIZE];
	if(!Q.elem) return ERROR;
	Q.front=Q.real=0;
	return OK;
} 
status push(sqQueue &Q,ElemType e)
{
	if((Q.real+1)%MAXSIZE==Q.front) return ERROR;
	Q.elem[Q.real]=e;
	Q.real=(Q.real+1)%MAXSIZE;
	return OK;
}
status pop(sqQueue &Q,ElemType e)
{
	if(Q.front==Q.real) return ERROR;
	e=Q.elem[Q.front];
	Q.front=(Q.front+1)%MAXSIZE;
	return OK;
}
status Get(sqQueue &Q,ElemType e)
{
	if(Q.front==Q.real) return ERROR;
	e=Q.elem[Q.front];
	return OK;
}
status show(sqQueue &Q)
{
	int i=Q.front;
	if(Q.front==Q.real)  cout<<"队列为空"<<endl;
	while(i!=Q.real)
	{
		cout<<Q.elem[i]<<" ";
		i=(i+1)%MAXSIZE;
	}
	return OK;
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值