当前位置: 首页 > news >正文

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>  //引入头文件
using namespace std;typedef int Elemtype;#define Maxsize 5
#define ERROR 0
#define OK    1typedef 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;
}

在这里插入图片描述

#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;
}
http://www.lryc.cn/news/180774.html

相关文章:

  • SSM - Springboot - MyBatis-Plus 全栈体系(十六)
  • k8s--storageClass自动创建PV
  • 7.3 调用函数
  • 如果使用pprof来进行性能的观测和优化
  • 在移动固态硬盘上安装Ubuntu系统和ROS2
  • 【iptables 实战】02 iptables常用命令
  • webview_flutter
  • 【GESP考级C++】1级样题 闰年统计
  • CentOS密码重置
  • Tomcat Servlet
  • 国庆day2---select实现服务器并发
  • Grafana 开源了一款 eBPF 采集器 Beyla
  • 亲测可用国产GPT人工智能
  • 适配器模式详解和实现(设计模式 四)
  • IDEA的使用
  • CSS详细基础(二)文本样式
  • win10系统任务栏图标变成白色的解决办法
  • hadoop生态现状、介绍、部署
  • 二、EFCore 数据库表的创建和迁移
  • 在nodejs中使用typescript
  • 数据结构与算法基础(青岛大学-王卓)(8)
  • 【生物信息学】使用谱聚类(Spectral Clustering)算法进行聚类分析
  • CSS基础语法第二天
  • ThreeJS - 封装一个GLB模型展示组件(TypeScript)
  • HashMap面试题
  • Java编程技巧:swagger2、knif4j集成SpringBoot或者SpringCloud项目
  • 第三章:最新版零基础学习 PYTHON 教程(第九节 - Python 运算符—Python 中的除法运算符)
  • 【python】导出mysql数据,输出excel!
  • 【Java 进阶篇】JDBC ResultSet 遍历结果集详解
  • 华为数通方向HCIP-DataCom H12-831题库(单选题:161-180)