# 集美大学课程实验报告-实验3:栈和队列

集美大学课程实验报告-实验3:栈和队列

项目名称 内容
课程名称 数据结构
班级 网安2411
指导教师 郑如滨
学生姓名 李斌财
学号 202421336021
实验项目名称 栈和队列
上机实践日期
上机实践时间 2学时

一、目的(本次实验所涉及并要求掌握的知识点)

掌握STL中栈和队列的基本存储结构
掌握STL中string的使用
熟练掌握栈(stack)和队列(queue)的基本使用
掌握栈和队列的一些典型应用

二、实验内容与设计思想

题目1:符号配对

    cout << "no" << endl;
}

函数代码

#include <iostream>
#include <stack>
#include <string>

using namespace std;
bool isMatch(char a, char b) {
    if (a == '(' && b == ')') return true;
    if (a == '[' && b == ']') return true;
    if (a == '{' && b == '}') return true;
    return false;
}
int main() {
    string expression;
    getline(cin, expression);
    stack<char> s;
    for (char c : expression) {
        // 如果是左括号,压入栈中
        if (c == '(' || c == '[' || c == '{') {
            s.push(c);
        }
        // 如果是右括号,检查是否匹配
        else if (c == ')' || c == ']' || c == '}') {
            if (s.empty()) {
                // 栈为空,没有匹配的左括号
                cout << "no" << endl;
                return 0;
            } else {
                char top = s.top();
                s.pop();
                if (!isMatch(top, c)) {
                    cout << top << endl;
                    cout << "no" << endl;
                    return 0;
                }
            }
        }
    }
    // 遍历结束
    if (s.empty()) {
        cout << "yes" << endl; // 栈为空,说明所有括号都匹配
    } else {
        // 栈不为空,输出栈顶元素并换行输出 no
        cout << s.top() << endl;

    return 0;
}
时间复杂度:O(n)
空间复杂度:O(n)
### 题目2:使用stack将以下递归程序转化为非递归程序(可申请检查加分)
void test(int &sum)
{ 
    int x; 
    cin>>x; 
    if (x==0) sum = 0;  
    else {
        test(sum);
        sum+=x;
     } 
     cout<<sum; 
}
改写后的代码:
#include <iostream>
#include <stack>
using namespace std;

void test(int& sum) {
    stack<int> s;
    int x;
    while (true) {
        cin >> x;
        if (x == 0) {
            break; 
        }
        s.push(x); 
    }
    while (!s.empty()) {
        x = s.top(); 
        s.pop(); 
        sum += x; 
    }
    cout << sum << endl; 
}
时间复杂度:O(n)
空间复杂度:O(n)
### 题目3:银行业务队列简单模拟
**函数代码**
#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n, k;
    cin >> n;
    queue<int> a, b;
    for (int i = 0; i < n; i++) {
        cin >> k;
        if (k % 2 == 0) {
            b.push(k); // 偶数编号的顾客去B窗口
        } else {
            a.push(k); // 奇数编号的顾客去A窗口
        }
    }

    // 处理A窗口和B窗口的顾客
    while (!a.empty() || !b.empty()) {
        if (!a.empty()) {
            cout << a.front();
            a.pop();
            if (!a.empty() || !b.empty()) cout << " ";
        }
        if (!a.empty()) {
            cout << a.front();
            a.pop();
            if (!a.empty() || !b.empty()) cout << " ";
        }
        if (!b.empty()) {
            cout << b.front();
            b.pop();
            if (!a.empty() || !b.empty()) cout << " "; 
        }
    }

    return 0;
}
时间复杂度:O(n)
空间复杂度:O(n)

三、实验使用环境(本次实验所使用的平台和相关软件)

  • 操作系统:Windows 11
  • 编程语言:C++
  • 开发工具:PTA,visual studio 2022
  • 编译器:gcc

四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)





五、实验小结(实验中遇到的问题及解决过程、实验体会和收获)

遇到的问题及解决方法:

  1. 问题:银行问题中空格处理
    • 解决方法: if (!a.empty() || !b.empty()) cout << " ";
      }虽然很丑陋但还是通过了;

实验体会和收获:

学会了栈(stack)和队列(queue)的基本使用


六、附件(参考文献和相关资料)

posted @ 2025-03-23 12:01  救苦救难观世音  阅读(25)  评论(0)    收藏  举报