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











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;
}
相关推荐
Hello_Embed42 分钟前
FreeRTOS 入门(四):堆的核心原理
数据结构·笔记·学习·链表·freertos·
虹科测试测量1 小时前
德思特干货 | 单通道、多通道衰减器与衰减矩阵:如何选择合适的衰减方案
服务器·测试工具·算法·矩阵
mit6.8242 小时前
py期中实验选题:实现天气预测
python·算法
烧冻鸡翅QAQ2 小时前
考研408笔记——数据结构
数据结构·笔记·考研
xuehaikj2 小时前
YOLOv8多场景人物识别定位与改进ASF-DySample算法详解
算法·yolo·目标跟踪
异步的告白2 小时前
C语言-数据结构-2-单链表程序-增删改查
c语言·开发语言·数据结构
Wenhao.2 小时前
LeetCode 救生艇
算法·leetcode·golang
超级无敌大学霸2 小时前
二分查找和辗转相除法
c语言·算法
夏鹏今天学习了吗2 小时前
【LeetCode热题100(69/100)】字符串解码
linux·算法·leetcode
普通网友2 小时前
内存对齐与缓存友好设计
开发语言·c++·算法