实验3

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

项目名称 内容
课程名称 数据结构
班级 网安2411
指导教师 郑如滨
学生姓名 于鸿硕
学号 202421336018
实验项目名称 实验3:栈与队列
上机实践日期
上机实践时间 2学时


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

  • 掌握stack&queue的操作方法 熟悉string的使用
    尝试递归函数算法的使用 了解函调和栈的关系

二、实验内容与设计思想

#include <iostream>
using namespace std;
    return 0

任务3:栈的运用(选择符号配对题)

函数伪代码与代码

//伪代码:
一、设计比较函数cmp检查括号是否匹配
    if(a[==b])return1
    其他匹配情况....return1
    return0
二、设计主函数比较
    intmain
    if左括号pop入栈
    elseif右括号 匹配入栈 否则进栈
    后将栈中元素push出并cmp
    cout<<yes||no

//代码:
#include<iostream>
#include<stack>
#include<string>
using namespace std;

bool cmp(char a, char b)
{
    if (a == '/' && b == '*')
        return 1;
    if (a == '[' && b == ']')
        return 1;
    if (a == '{' && b == '}')
        return 1;
    if (a == '(' && b == ')')
        return 1;
    return 0;
}

int main() {
    string input;
    getline(cin, input);
    char s[200];
    int top = -1;
    bool hasBracket = false;
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == '(' || input[i] == '[' || input[i] == '{') {
            hasBracket = true;
            s[++top] = input[i];
        }
        else if (input[i] == ')' || input[i] == ']' || input[i] == '}') {
            hasBracket = true;
            if (top == -1 || !cmp(s[top], input[i])) {
                cout << "NO" << endl;
                if (input[i] == '(' || input[i] == '[' || input[i] == '{') {
                    cout << input[i] << endl;
                }
                else {
                    cout << input[i] << endl;
                }
                return 0;
            }
            else {
                top--;
            }
        }
    }
    if (!hasBracket) {
        cout << "no" << endl;
        return 0;
    }
    if (top == -1) {
        cout << "yes" << endl;
    }
    else {
        cout << s[top] << endl;
        cout << "no" << endl;
    }
    return 0;
}

实验中发现,我使用hasBracket函数判断是否输入无括号符导致空栈的功能好像不能完全实现,pintia返回中有一个测试点错误,但我本机测试里确实可以返回“no”

任务4:递归程序编写(选择4.1)

//伪代码
一、findmax函数
    处理空链表和单节点链表
    int submax调用递归=findmax
    return最大值(subamx||head->next)
二、countnode函数
    处理空链表
    return递归
三、findaverage函数
    处理空链表
    双重嵌套findavergae和countnode
    int sum&&count计数
    return sum/count
四、主函数
    新建链表
    cout<<findmax&&countnode&&findaverage

//代码
#include<iostream>
using namespace std;

struct ListNode {
    int data;
    ListNode* next;
    ListNode(int x) : data(x), next(nullptr) {}
};

// 求链表中的最大整数
int findMax(ListNode* head) {
    if (head == nullptr) {
        return NULL;
    }
    if (head->next == nullptr) {
        return head->data;
    }
    int subMax = findMax(head->next);
    return (head->data> subMax) ? head->data : subMax;
}

// 求链表的结点个数
int countNodes(ListNode* head) {
    if (head == nullptr) {
        return 0;
    }
    return 1 + countNodes(head->next);
}

// 求链表中所有节点数据的平均值
double findAverage(ListNode* head) {
    if (head == nullptr) {
        return 0;
    }
    int count = 1;
    double sum = head->data;
    double subAverage = findAverage(head->next);
    int subCount = countNodes(head->next);
    sum += subAverage * subCount;
    count += subCount;
    return sum / count;
}

int main() {
    ListNode* head = new ListNode(1);
    head->next = new ListNode(2);
    head->next->next = new ListNode(3);
    head->next->next->next = new ListNode(4);
    head->next->next->next->next = new ListNode(5);

    cout << "链表中的最大整数: " << findMax(head) << std::endl;
    cout << "链表的结点个数: " << countNodes(head) << std::endl;
    cout << "链表中所有节点数据的平均值: " << findAverage(head) << std::endl;

    return 0;
}

任务5:队列的应用(银行业务队列简单模拟

//代码
#include <iostream>
using namespace std;
#include <cstdlib>
#define ERROR -1

struct que {
    int Data[1000];
    int Front, Rear;
};

typedef struct que* QNode;
typedef QNode Queue;

int main() {
    int n;
    Queue A = (Queue)malloc(sizeof(struct que));
    Queue B = (Queue)malloc(sizeof(struct que));
    A->Rear = B->Rear = A->Front = B->Front = 0;
    cin >> n;
    while (n--) {
        int num;
        cin >> num;
        if (num % 2) {
            A->Data[A->Rear++] = num;
        }
        else {
            B->Data[B->Rear++] = num;
        }
    }
    int cnt = 0;
    if (A->Data[0]) {
        cnt = 1;
    }
    int i = 0;
    while (A->Front != A->Rear) {
        int a2 = 2;
        while (a2-- && A->Front != A->Rear) {
            if (i++) {
                cout << " " << A->Data[A->Front++];
            }
            else {
                cout << A->Data[A->Front++];
            }
        }
        if (B->Front != B->Rear) {
            cout << " " << B->Data[B->Front++];
        }
    }
    while (B->Rear != B->Front) {
        if (cnt == 0) {
            cout << B->Data[B->Front++];
            cnt++;
        }
        else {
            cout << " " << B->Data[B->Front++];
        }
    }
    return 0;
}

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

以下请根据实际情况编写

  • 操作系统:Windows 11
  • 编程语言:C++
  • 开发工具:visual studio2022 &pintia
  • 编译器:g++

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

任务3:栈的运用(选择符号配对题)

本机运行截图
https://www.cnblogs.com/kinthyu/gallery/image/511958.html

img

PTA提交截图
https://www.cnblogs.com/kinthyu/gallery/image/511959.html

img

任务4:递归程序编写

本机运行截图

https://images.cnblogs.com/cnblogs_com/blogs/841920/galleries/2450878/o_250325022236_image.png

img

任务5:队列的应用(银行业务队列简单模拟

本机运行截图

https://images.cnblogs.com/cnblogs_com/blogs/841920/galleries/2450878/o_250325025540_image.png

img

PTA提交截图

https://images.cnblogs.com/cnblogs_com/blogs/841920/galleries/2450878/o_250325025449_image.png

img


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

遇到的问题及解决方法:

实验过程中最大的问题是理解递归问题时脑子转不过弯来,会不能理解这种算法的运行逻辑。后来通过在纸上画图,用箭头表示前套关系这种方式基本理解了递归算法的逻辑

实验体会和收获:

实验过程中学习了stack&queue的用法,掌握了他们的基本逻辑和使用注意事项,具备了通过这两种算法编写功能函数的能力。并学会了递归算法的逻辑和内涵。


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

posted @ 2025-03-25 11:02  KinthYu  阅读(63)  评论(0)    收藏  举报