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

栈与队列---大学数据结构 #报告模板#集美大学#基础版#招学习搭子 私聊#PTA

大学课堂学习

一.目的
掌握STL中stack与queue的使用
string的使用
掌握栈与队列的应用
理解递归函数的编写
函调用与栈的关系

二.任务要求

1.栈的应用(以数值转换为例)
image
image

其代码

include

include

include

using namespace std;

int main() {
string s;
getline(cin, s); // 读取一行输入
stack st;

for (char c : s) {if (c == '(') {st.push(c); // 左括号入栈} else if (c == ')') { // 遇到右括号if (st.empty()) { // 栈空,无左括号匹配cout << "no" << endl;return 0;}char top = st.top();if (top == '(') { // 匹配成功st.pop();} else { // 不匹配,输出栈顶+nocout << top << endl;cout << "no" << endl;return 0;}}
}// 遍历结束,检查栈是否为空
if (st.empty()) {cout << "YES" << endl;
} 
else {// 栈非空,输出剩余左括号+nocout << st.top() << endl;cout << "no" << endl;
}return 0;

}

2.递归编程编写

image

include

include

using namespace std;

// 栈帧:标记当前执行状态,存储x值
struct StackFrame {
int state; // 0: 未执行递归调用 1: 递归调用已返回
int x; // 存储当前读取的x
StackFrame(int s, int val) : state(s), x(val) {}
};

void test(int &sum) {
stack st;
int x;

// 初始入栈:模拟第一次调用test
st.emplace(0, 0);while (!st.empty()) {auto &frame = st.top();if (frame.state == 0) {// 状态0:执行递归调用前的逻辑(读x)cin >> x;frame.x = x;if (x == 0) {sum = 0;cout << sum;st.pop();  // 直接出栈,无递归} else {frame.state = 1;  // 标记为“待执行返回后逻辑”st.emplace(0, 0);  // 入栈模拟递归调用}} else {// 状态1:执行递归返回后的逻辑(sum += x,输出)sum += frame.x;cout << sum;st.pop();}
}

}

int main() {
int sum = 0;
test(sum);
return 0;
}

3.队列的应用(以银行业务队列简单模拟)
image

include

include

using namespace std;

int main() {
queue qA, qB;
int N, num;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> num;
if (num % 2 == 1)
qA.push(num);
else
qB.push(num);
}
bool first = true;
while (!qA.empty() || !qB.empty()) {
int cnt = 0;
while (!qA.empty() && cnt < 2) {
if (!first) cout << " ";
first = false;
cout << qA.front();
qA.pop();
cnt++;
}
if (!qB.empty())
{
if (!first) cout << " ";
first = false;
cout << qB.front();
qB.pop();
}
}
return 0;
}

image

4.走迷宫
image

include

include

include

include

using namespace std;

// 坐标结构体
struct Point {
int x, y;
Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};

// 方向数组:上、右、下、左
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

// 迷宫大小(对应你图中的8行9列)
const int ROW = 8;
const int COL = 9;

// 迷宫地图:1=墙(棕色),0=通路(白色)
int maze[ROW][COL] = {
{1, 0, 0, 1, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 1}
};

// 访问标记数组
bool visited[ROW][COL] = {false};

// 栈实现DFS迷宫求解
stack solveMaze(Point start, Point end) {
stack path;
path.push(start);
visited[start.x][start.y] = true;

while (!path.empty()) {Point curr = path.top();// 到达终点if (curr == end) {return path;}bool hasNext = false;// 遍历四个方向for (int i = 0; i < 4; ++i) {int nx = curr.x + dx[i];int ny = curr.y + dy[i];// 检查边界、是否为墙、是否已访问if (nx >= 0 && nx < ROW && ny >= 0 && ny < COL && maze[nx][ny] == 0 && !visited[nx][ny]) {visited[nx][ny] = true;path.push(Point(nx, ny));hasNext = true;break;}}// 无可用方向,回溯(出栈)if (!hasNext) {path.pop();}
}return stack<Point>(); // 无通路,返回空栈

}

// 打印迷宫
void printMaze() {
cout << "迷宫地图(1=墙,0=通路):" << endl;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
cout << maze[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

// 打印路径
void printPath(stack path) {
if (path.empty()) {
cout << "无通路!" << endl;
return;
}

vector<Point> vec;
while (!path.empty()) {vec.push_back(path.top());path.pop();
}
reverse(vec.begin(), vec.end());cout << "找到通路,路径坐标(行,列,从0开始):" << endl;
for (size_t i = 0; i < vec.size(); ++i) {cout << "(" << vec[i].x << "," << vec[i].y << ")";if (i != vec.size() - 1) cout << " → ";
}
cout << endl << endl;// 可视化路径(*代表路径)
cout << "路径可视化:" << endl;
vector<vector<char>> show(ROW, vector<char>(COL, '■')); // ■代表墙
for (int i = 0; i < ROW; ++i) {for (int j = 0; j < COL; ++j) {if (maze[i][j] == 0) show[i][j] = '□'; // □代表通路}
}
for (auto& p : vec) {show[p.x][p.y] = '*'; // *代表路径
}
for (int i = 0; i < ROW; ++i) {for (int j = 0; j < COL; ++j) {cout << setw(2) << show[i][j];}cout << endl;
}

}

int main() {
// 起点:(0,1)(对应你图中左上角入口)
// 终点:(7,7)(对应你图中右下角出口)
Point start(0, 1);
Point end(7, 7);

printMaze();
stack<Point> path = solveMaze(start, end);
printPath(path);return 0;

}
时间复杂度:O(ROW×COL),每个格子最多访问一次
空间复杂度:O(ROW×COL),栈最多存储所有通路格子
三. 实验使用环境

Visual Studio 2022

五、实验小结
学习搭子真的很重要,有意者私聊(qq 702599337)

http://www.jsqmd.com/news/646918/

相关文章:

  • 如何永久备份微信聊天记录:WeChatExporter完整教程
  • 基于模糊势场的多智能体协同编队控制仿真研究附Matlab代码
  • 3大核心功能+4种性能模式:华硕笔记本终极轻量控制方案G-Helper深度解析
  • 别再只盯着Transformer了!用MOE(专家混合)搞定亿级参数时序预测,附Time-300B数据集使用指南
  • CVPR 2024 热门数据集解析与应用指南
  • MRI脉冲序列设计的基石:手把手拆解布洛赫方程中的旋转矩阵(附Python模拟代码)
  • 如何在3分钟内为Windows 11 24H2 LTSC系统一键安装微软商店:完整免费解决方案指南
  • 从Maya到Unity的完整管线:角色模型导入+骨骼动画配置全流程实操
  • 多模态大模型能效比(Tokens/Watt)提升2.8倍的工业级实践(覆盖ViT+LLM联合剪枝、模态门控蒸馏、内存带宽自适应预取)
  • 3分钟学会AI音频修复:让模糊录音重获清晰生命的完整指南
  • 多模态大模型如何边学边用不遗忘?——揭秘动态参数隔离+梯度正交约束的双重增量稳态机制
  • 你的 Vue 3 defineProps(),VuReact 会编译成什么样的 React?
  • 基于CCA和VTP实现路径跟踪控制胡萝卜追逐算法和虚拟目标点附Matlab代码
  • 牛客:aoe还是单体
  • Gradle仓库配置优化:用阿里云镜像替代mavenCentral()、jcenter()和google()
  • Clock Gating技术解析:如何有效降低芯片动态功耗
  • 【2026年华为暑期实习-非AI方向(通软嵌软测试算法数据科学)-4月15日-第二题(100分)- 异或树】(题目+思路+JavaC++Python解析+在线测试)
  • 多模态长尾泛化能力跃迁方案(附GitHub千星工具包+3类长尾benchmark原始数据集)
  • G-Helper深度评测:华硕笔记本性能调优的终极轻量解决方案
  • Leaflet实战:从零构建交互式地图应用
  • Xournal++手写笔记软件:免费开源的多平台数字笔记终极指南
  • 2026 北京家装价值观察:丰盛谦诚装饰,以口碑与诚信领跑京城家装新高度 - 资讯焦点
  • 实测DeepSeek AI测试工具:5分钟生成Java单元测试用例(附避坑指南)
  • 【2026年华为暑期实习-非AI方向(通软嵌软测试算法数据科学)-4月15日-第三题(100分)- 实现一个窗口系统】(题目+思路+JavaC++Python解析+在线测试)
  • 多模态大模型模型并行训练黄金公式(FLOPs/Token × Comm-BW × Modality Alignment Ratio = 实际加速上限)
  • 多模态新闻生成爆发前夜,算法偏见、版权归属与实时性三重危机全解析,一线AI编辑部实测方案
  • 2026拖地好用的地板清洁剂推荐哪款?全能去污、高效抑菌、速干护面全维度实测 - 资讯焦点
  • 思源宋体TTF:7种字重打造专业级中文排版新标准
  • 3步打造专业级象棋AI助手:深度学习智能连线实战指南
  • 酷安UWP桌面客户端:在Windows上体验完整酷安社区的终极指南