数据结构与算法课后题-第三章(顺序队和链队)











cpp 复制代码
#include <iostream>  //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 5
#define ERROR 0
#define OK    1

typedef struct
{
	Elemtype data[Maxsize];
	int front, rear;
	int tag;
}SqQueue;

void InitQueue(SqQueue& Q)  //初始化队列
{
	Q.rear = Q.front = 0;
	Q.tag = 0;
}

bool isEmpty(SqQueue Q)  //判断队空
{
	if (Q.rear == Q.front&& Q.tag==0 ) return OK;
	else return ERROR;
}

bool EnQueue(SqQueue& Q, Elemtype x)
{
	if (Q.front==Q.rear&&Q.tag==1)  //队满
	{
		cout << "堆满啦,请腾出些许空间" << endl;
		return ERROR;  //队满报错
	}
	Q.data[Q.rear] = x;
	Q.rear = (Q.rear + 1) % Maxsize;
	Q.tag = 1;
	return OK;
}

bool DeQueue(SqQueue& Q, Elemtype& x)  //队空报错
{
	if (Q.rear == Q.front&&Q.tag==0) return ERROR;
	x = Q.data[Q.front];
	Q.front = (Q.front + 1) % Maxsize;
	Q.tag = 0;
	return OK;
}

int main(void)
{
	SqQueue Q;
	InitQueue(Q);
	EnQueue(Q, 1);
	EnQueue(Q, 2);
	EnQueue(Q, 3);
	EnQueue(Q, 4);
	EnQueue(Q, 5);
	for (int i = 0; i < Maxsize; i++)
		printf("data[%d]=%d\n", i, Q.data[i]);
	return 0;
}
cpp 复制代码
#include <iostream>  //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 5
#define ERROR 0
#define OK    1

//====================队列---这样是循环队列,需要牺牲一位====================//
typedef struct
{
	Elemtype data[Maxsize];
	int front, rear;
}SqQueue;

void InitQueue(SqQueue& Q)  //初始化队列
{
	Q.rear = Q.front = 0;
}

bool QueueEmpty(SqQueue Q)  //判断队空
{
	if (Q.rear == Q.front) return OK;
	else return ERROR;
}

bool EnQueue(SqQueue& Q, Elemtype x)  //进队
{
	if ((Q.rear + 1) % Maxsize == Q.front) //判断队列是否满
	{
		cout << "堆满啦,请释放一些空间" << endl;
		return ERROR;  //队满报错
	}
	Q.data[Q.rear] = x;
	Q.rear = (Q.rear + 1) % Maxsize;
	return OK;
}

bool DeQueue(SqQueue& Q, Elemtype& x) //退队
{
	if (Q.rear == Q.front) return ERROR; //判断队列是为空
	x = Q.data[Q.front];
	Q.front = (Q.front + 1) % Maxsize;
	return OK;
}
//=====================================================================//

//========================堆栈---这样是顺序堆栈========================//
typedef struct
{
	Elemtype data[Maxsize];
	int top;
}SqStack;

void InitStack(SqStack& S)
{
	S.top = -1;
}
bool StackEmpty(SqStack S)
{
	if (S.top == -1)   //堆空
		return OK;
	else              //不空
		return ERROR;
}

bool Push(SqStack& S, Elemtype x)
{
	if (S.top == Maxsize - 1)
		return ERROR;
	S.data[++S.top] = x;
	return OK;
}

bool Pop(SqStack& S, Elemtype& x)
{
	if (S.top == -1)
		return ERROR;
	x = S.data[S.top--];
	return OK;
}

bool GetTop(SqStack& S, Elemtype& x)
{
	if (S.top == -1)
		return ERROR;
	x = S.data[S.top];
	return OK;
}
//=====================================================================//

void Inverse(SqStack& S, SqQueue& Q)
{
	int x = 0;
	while (!QueueEmpty(Q))
	{
		DeQueue(Q,x);
		Push(S, x);
	}
	Q.rear = Q.front = 0;
	while (!StackEmpty(S))
	{
		Pop(S, x);
		EnQueue(Q, x);
	}
}
int main(void)
{
	SqQueue Q;
	SqStack S;
	InitQueue(Q);
	InitStack(S);
	EnQueue(Q, 1);
	EnQueue(Q, 2);
	EnQueue(Q, 3);
	EnQueue(Q, 4);
	for (int i = 0; i < Maxsize; i++)
		printf("data[%d]=%d\n", i, Q.data[i]);
	Inverse(S, Q);
	for (int i = 0; i < Maxsize; i++)
		printf("data[%d]=%d\n", i, Q.data[i]);
	return 0;
}
相关推荐
Aurorar0rua18 分钟前
C Primer Plus Notes 09
java·c语言·算法
我不是QI3 小时前
DES 加密算法:核心组件、加解密流程与安全特性
经验分享·算法·安全·网络安全·密码学
前端小刘哥3 小时前
新版视频直播点播EasyDSS平台,让跨团队沟通高效又顺畅
算法
明月(Alioo)4 小时前
机器学习入门,无监督学习之K-Means聚类算法完全指南:面向Java开发者的Python实现详解
python·算法·机器学习
叶梅树4 小时前
从零构建A股量化交易工具:基于Qlib的全栈系统指南
前端·后端·算法
lingran__4 小时前
算法沉淀第三天(统计二进制中1的个数 两个整数二进制位不同个数)
c++·算法
MicroTech20255 小时前
微算法科技MLGO推出隐私感知联合DNN模型部署和分区优化技术,开启协作边缘推理新时代
科技·算法·dnn
小冯记录编程5 小时前
深入解析C++ for循环原理
开发语言·c++·算法
chenchihwen6 小时前
深度解析RAG系统中的PDF解析模块:Docling集成与并行处理实践
python·算法·pdf
Chloeis Syntax7 小时前
栈和队列笔记2025-10-12
java·数据结构·笔记·