顺序队列(C/C++)

一般队列

0.定义队列的基本结构

/*0.定义队列的基本结构*/
typedef struct {
	ElemType data[Maxsize]; //用静态数组存放队列元素
	int front, rear; //队头指针与队尾指针
}SqQueue;

1.初始化队列

/*1.初始化队列*/
void InitQueue(SqQueue& Q)
{//初试时,队头、队尾指针指向0
	Q.front = Q.rear = 0;
}

2.判断队列空

/*2.判断队列空*/
Status QueueEmpty(SqQueue Q)
{
	if (Q.rear == Q.front)
		return true;
	else
		return false;
}

3.入队

/*3.入队*/
Status EnQueue(SqQueue& Q, ElemType x)
{
	if (Q.rear == Maxsize) //会存在假上溢
		return false;//队满则报错
	Q.data[Q.rear] = x; //将x插入队尾
	Q.rear++;
	return true;
}

4.出队

/*4.出队*/
Status DeQueue(SqQueue& Q, ElemType& x)
{
	if (Q.front == Q.rear)
		return false; //队空则报错
	x = Q.data[Q.front]; //将队头元素赋给x
	Q.front++;
	return true;
}

顺序队列的完整代码

/*实现静态队列的基本操作*/
#include<stdio.h>
#include<stdlib.h>

#define Maxsize 20

typedef int ElemType;
typedef bool Status;

/*0.定义队列的基本结构*/
typedef struct {
	ElemType data[Maxsize]; //用静态数组存放队列元素
	int front, rear; //队头指针与队尾指针
}SqQueue;

/*1.初始化队列*/
void InitQueue(SqQueue& Q)
{//初试时,队头、队尾指针指向0
	Q.front = Q.rear = 0;
}

/*2.判断队列空*/
Status QueueEmpty(SqQueue Q)
{
	if (Q.rear == Q.front)
		return true;
	else
		false;
}

/*3.入队*/
Status EnQueue(SqQueue& Q, ElemType x)
{
	if (Q.rear == Maxsize) //会存在假上溢
		return false;//队满则报错
	Q.data[Q.rear] = x; //将x插入队尾
	Q.rear++;
	return true;
}

/*4.出队*/
Status DeQueue(SqQueue& Q, ElemType& x)
{
	if (Q.front == Q.rear)
		return false; //队空则报错
	x = Q.data[Q.front]; //将队头元素赋给x
	Q.front++;
	return true;
}

int main()
{
	SqQueue Q;
	ElemType x;
	InitQueue(Q);
	EnQueue(Q, 3);
	EnQueue(Q, 6);
	EnQueue(Q, 8);
	EnQueue(Q, 9);
	DeQueue(Q, x);
	printf("%d\n", x);
	return 0;
}

循环队列

0.定义循环队列的基本结构

/*0.定义循环队列的基本结构*/
typedef struct {
	ElemType data[Maxsize]; //用静态数组存放队列元素
	int front, rear; //队头指针与队尾指针
}SqQueue;

1.初始化队列

/*1.初始化队列*/
void InitQueue(SqQueue& Q)
{//初试时,队头、队尾指针指向0
	Q.front = Q.rear = 0;
}

2.判断队列空

/*2.判断队列空*/
Status QueueEmpty(SqQueue Q)
{
	if (Q.rear == Q.front) //队空条件
		return true;
	else
		return false;
}

3.入队

/*3.入队*/
Status EnQueue(SqQueue& Q, ElemType x)
{ //队头指针在队尾指针的下一个位置作为队满的标志
	if ((Q.rear + 1) % Maxsize == Q.front)
		return false;//队满则报错
	Q.data[Q.rear] = x; //将x插入队尾
	Q.rear = (Q.rear + 1) % Maxsize; //队尾指针加1取模
	return true;
}

4.出队

/*4.出队*/
Status DeQueue(SqQueue& Q, ElemType& x)
{
	if (Q.front == Q.rear)
		return false; //队空则报错
	x = Q.data[Q.front]; //将队头元素赋给x
	Q.front = (Q.front + 1) % Maxsize; //队头指针加1取模
	return true;
}

循环队列的完整代码

/*实现循环队列的基本操作*/
#include<stdio.h>
#include<stdlib.h>

#define Maxsize 20

typedef int ElemType;
typedef bool Status;

/*0.定义循环队列的基本结构*/
typedef struct {
	ElemType data[Maxsize]; //用静态数组存放队列元素
	int front, rear; //队头指针与队尾指针
}SqQueue;

/*1.初始化队列*/
void InitQueue(SqQueue& Q)
{//初试时,队头、队尾指针指向0
	Q.front = Q.rear = 0;
}

/*2.判断队列空*/
Status QueueEmpty(SqQueue Q)
{
	if (Q.rear == Q.front) //队空条件
		return true;
	else
		return false;
}

/*3.入队*/
Status EnQueue(SqQueue& Q, ElemType x)
{ //队头指针在队尾指针的下一个位置作为队满的标志
	if ((Q.rear + 1) % Maxsize == Q.front)
		return false;//队满则报错
	Q.data[Q.rear] = x; //将x插入队尾
	Q.rear = (Q.rear + 1) % Maxsize; //队尾指针加1取模
	return true;
}

/*4.出队*/
Status DeQueue(SqQueue& Q, ElemType& x)
{
	if (Q.front == Q.rear)
		return false; //队空则报错
	x = Q.data[Q.front]; //将队头元素赋给x
	Q.front = (Q.front + 1) % Maxsize; //队头指针加1取模
	return true;
}

int main()
{
	SqQueue Q;
	ElemType x;
	InitQueue(Q);
	EnQueue(Q, 3);
	DeQueue(Q, x);
	printf("%d\n", x);
	return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值