实验3
集美大学课程实验报告-实验3:栈、队列与递归
| 项目名称 | 内容 |
|---|---|
| 课程名称 | 数据结构 |
| 班级 | 网安2512 |
| 学号 | 202521336053 |
| 实验项目名称 | 栈、队列与递归 |
| 上机实践日期 | 2026年4月12日 |
| 上机实践时间 | 2学时 |
一、目的(本次实验所涉及并要求掌握的知识点)
- 学习栈与队列的创建与应用。
- 掌握递归在栈与队列的应用。
- 掌握string的使用。
二、实验内容与设计思想
题目1:栈的应用
函数相关伪代码
算法:括号匹配
输入:一个字符串str
输出:若完全匹配则输出“yes”;否则若栈非空则输出栈顶元素后换行输出“no”;若栈空则直接输出“no”
1.初始化一个空栈S
2.对于str中的每一个字符ch:
2.1如果ch是左括号‘(’或者‘[’或者‘{’:
将ch压入栈S
2.2否则如果ch是右括号‘)’或者‘]’或者‘}’:
如果栈S为空:
输出“no”
结束算法
否则:
topchar=S的栈顶元素
如果(ch是‘)’且topchar是‘(’)或者
(ch是‘]’且topchar是‘[’)或者
(ch是‘}’且topchar是‘{’):
弹出栈顶元素
否则:
弹出topchar
换行输出“no”
结束算法
2.3否则(其他字符)
忽略,继续遍历
3.遍历结束后:
如果栈S为空:
输出“yes”
否则:
输出S的栈顶元素
换行输出“no”
函数代码
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin,s);
stack<char>st;
for(char c:s)
{
if(c=='('||c=='['||c=='{')
{
st.push(c);
}
else if(c==')'||c==']'||c=='}')
{
if(st.empty())
{
cout<<"no"<<endl;
return 0;
}
char top=st.top();
if((c==')'&&top=='(')||(c==']'&&top=='[')||(c=='}'&&top=='{'))
{
st.pop();
}else
{
cout<<top<<endl;
cout<<"no"<<endl;
return 0;
}
}
}
if(st.empty())
{
cout<<"yes"<<endl;
}else
{
cout<<st.top()<<endl;
cout<<"no"<<endl;
}
return 0;
}
题目2:递归程序编写
函数相关伪代码
算法:getMax(head)
输入:头结点指针head(带头结点)
输出:链表中的最大整数,若链表为空则返回约定最小值
if head.next==null
return 约定最小值
else
return maxRecursive(head.next)
函数 maxRecursive(p)
if p.next==null
return p.data
else
subMax=maxRecursive(p.next)
if p.data>subMax
return p.data
else
return subMax
算法:getCount(head)
输入:头结点指针head
输出:链表中数据节点的个数
if head.next==null
return 0
else
return countRecursive(head.next)
函数 countRecursive(p)
if p==null
return 0
else
return 1+countRecursive(p.next)
算法:getAverage(head)
输入:头结点指针head
输出:所有数据结点的平均值(double类型),空链表返回0.0
if head.next==null
return 0.0
else
sum=sumRecursive(head.next)
count=countRecursive(head.next)
return (double)sum/count
函数sumRecursive(p)
if p==null
return 0.0
else
return p.data+sumRecursive(p
.next)
函数代码
#include<climits>
#include<iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int val = 0, Node* nxt = NULL) :data(val), next(nxt) {}
};
int getMax(Node* p)
{
if (p == NULL)
{
return INT_MIN;
}
int subMax = getMax(p->next);
return (p->data > subMax) ? p->data : subMax;
}
int getCount(Node* p)
{
if (p == NULL)
{
return 0;
}
return 1 + getCount(p->next);
}
double getAverage(Node* p, int& count)
{
if (p == NULL)
{
count = 0;
return 0.0;
}
double subAvg = getAverage(p->next, count);
count++;
return (p->data + (count - 1) * subAvg) / count;
}
double getAverage(Node* head)
{
int count = 0;
return getAverage(head->next, count);
}
int main()
{
Node* head = new Node();
head->next = new Node(3);
head->next->next = new Node(1);
head->next->next->next = new Node(4);
Node* emptyHead = new Node();
int maxVal = getMax(head->next);
cout << "最大整数:" << maxVal << endl;
int maxEmpty = getMax(emptyHead->next);
if (maxEmpty == INT_MIN)
cout << "空链表无最大值" << endl;
int cnt = getCount(head->next);
cout << "结点个数:" << cnt << endl;
cout << "空链表结点个数:" << getCount(emptyHead->next) << endl;
double avg = getAverage(head);
cout << "平均值:" << avg << endl;
double avgEmpty = getAverage(emptyHead);
if (getCount(emptyHead->next) == 0)
cout << "空链表无法计算平均值" << endl;
return 0;
}
题目3:队列的应用
函数相关伪代码
算法:银行业务队列
输入:正整数N和N个客户编号
输出;按业务完成顺序输出的客户编号
1.读取N
2.初始化两个空队列A(奇数客户)B(偶数客户)
3.循环i=1到N
当x为奇数
将x入队A
否则
将x入队B
4.初始化空列表result,用于存储顺序编号
5.当队列A非空或者队列B非空时,执行:
如果队列A为非空
则弹出队头元素并加入result
如果队列A为非空
则弹出队头元素并加入result
如果队列B为非空
则弹出队头元素并加入result
6.输出result,中间需要有空格,末尾不用
7.结束
函数代码
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main()
{
int N;
cin>>N;
queue<int>A,B;
for(int i=0;i<N;i++)
{
int x;
cin>>x;
if(x%2==1)
{
A.push(x);
}else
{
B.push(x);
}
}
vector<int>result;
while(!A.empty()||!B.empty())
{
if(!A.empty())
{
result.push_back(A.front());
A.pop();
}
if(!A.empty())
{
result.push_back(A.front());
A.pop();
}
if(!B.empty())
{
result.push_back(B.front());
B.pop();
}
}
for(size_t i=0;i<result.size();i++)
{
if(i>0)
{
cout<<" ";
}
cout<<result[i];
}
cout<<endl;
return 0;
}
三、实验使用环境(本次实验所用的平台和相关软件)
- 操作系统:Windows 11专业版
- 编程语言:C++
- 开发工具:visual studio
四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)
题目1:符号配对
本机运行截图

PTA提交截图

题目2:递归程序
本机运行截图

题目3:银行业务队列
本机运行截图

**PTA提交截图

五、实验小结(实验中遇到的问题及解决过程、实验体会和收获)
遇到的问题以及解决方法
1.问题:程序编译不成功
-解决方法:把代码保存的方式换一下
2.问题:不知道出队入队怎么用代码表示
-解决方法:寻找AI进行询问
实验体会和收获:
- 学会了创建了栈与队列
- 学会了如何使用栈与队列在实际问题的应用
- 学会了如何编写递归程序
六、附件(参考文献和相关资料)
1.deepseek

浙公网安备 33010602011771号