• 【王道数据结构】第三章 栈和队列


    3.1 栈

    3.1.1 栈Stack的基本概念

    image-20220806212447950
    • 栈的定义
    image-20220806212630822 image-20220806212744055

    栈的数学性质:

    n 个不同的元素进栈,出栈元素不同排列的个数为
    1 n + 1 C 2 n n \frac{1}{n+1}C_{2n}^{n} n+11C2nn
    上述公式称为 卡特兰数

    栈的常考题型:

    image-20220806213640443
    • 栈的基本操作
    image-20220806213510503
    image-20220806214307002

    3.1.2 栈的顺序存储结构

    1、顺序栈的实现

    image-20220806214410540
    • 顺序栈的定义
    image-20220806214641653
    • 初始化操作

    image-20220806215059475

    有时初始时将 S.top 定义为0,即规定top指向栈顶元素的下一个存储单元。

    • 进栈操作

    image-20220806215813978

    • 出栈操作

    image-20220806215932739

    • 读取栈顶元素操作

    image-20220806220128203

    另一种方式(初始时 S.top = 0):

    image-20220806220443716

    image-20220806220503924

    2、共享栈

    两个栈的栈底分别设置在共享空间的两端,两个栈顶向共享空间的中间延申

    image-20220806221229001image-20220806221246521


    image-20220806221229001image-20220806221246521

    3.1.3 栈的链式存储结构

    image-20220806221403093

    回顾链表

    image-20220806221839693 image-20220806222055609

    链栈的定义:

    image-20220806222115530

    带头结点:

    // 链表结点定义
    typedef struct LinkNode{
        int data;
        struct LinkNode *next;
    } LNode, *LiStack;
    
    // 带头结点的初始化
    bool InitStack(LiStack &L){
        L = (LNode*)malloc(sizeof(LNode));
        if(L == NULL)
            return false;
        L -> next = NULL;
        return true;
    }
    
    // 进栈
    bool InsertNextNode(LiStack L, int e){
        if(L == NULL)
            return false;
        LNode *s = (LNode*)malloc(sizeof(LNode));
        if(s == NULL)
            return false;
        s -> data = e;
        s -> next = L -> next;
        L -> next = s;
        return true;
    }
    
    // 出栈
    bool DeleteNextNode(LiStack L){
        if(L -> next == NULL)
            return false;
        LNode *q = L -> next;
        L -> next = q -> next;
        free(q);
        return true;
    }
    
    // 获取栈顶元素
    bool GetTop(LiStack L, int &e){
        if(L -> next == NULL)
            return false;
        e = L -> next -> data;
        return true;
    }
    
    // 判断空
    bool isEmpty(LiStack L){
        if(L -> next == NULL)
            return true;
        return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    不带头结点:

    // 链表结点定义
    typedef struct LinkNode{
        int data;
        struct LinkNode *next;
    } LNode, *LiStack;
    
    // 不带头结点的初始化
    bool InitStack(LiStack &L){
        L = NULL;
        return true;
    }
    
    // 进栈
    bool InsertNextNode(LiStack L, int e){
        if(L == NULL)
            return false;
        LNode *s = (LNode*)malloc(sizeof(LNode));
        if(s == NULL)
            return false;
        s = L -> next;
        
    }
    
    // 出栈
    bool DeleteNextNode(LiStack L){
        if(L -> next == NULL)
            return false;
        LNode *q = L -> next;
        L -> next = q -> next;
        free(q);
        return true;
    }
    
    // 获取栈顶元素
    bool GetTop(LiStack L, int &e){
        if(L -> next == NULL)
            return false;
        e = L -> next -> data;
        return true;
    }
    
    // 判断空
    bool isEmpty(LiStack L){
        if(L == NULL)
            return true;
        return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    image-20220806225353837

    3.2 队列

    3.2.1 队列的基本概念

    image-20220822192219240
    • 定义:
    image-20220822192235006 image-20220822192554979
    • 队列的基本操作
    image-20220822192614524
    image-20220822192627003

    3.2.2 队列的顺序实现

    image-20220822192715882
    • 队列的顺序存储类型
    image-20220822192743206
    • 初始化操作
    image-20220822192807783
    • 入队操作
    image-20220822192833739 image-20220822192857713

    循环队列

    image-20220822192940395

    • 入队
    image-20220822193501337
    • 出队
    image-20220822193511836
    • 判断队列已满/已空
    image-20220822193536026 image-20220822193547851 image-20220822193602756
    • 其他

    image-20220822193621531

    image-20220822193636882

    image-20220822193651554


    image-20220822193702468

    3.2.3 队列的链式实现

    image-20220822193726169
    • 链式存储实现
    image-20220822193746291
    • 初始化
    image-20220822193803129 image-20220822193818428
    • 入队
    image-20220822193837876

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mA6RMDGV-1661179873646)(C:/Users/G/AppData/Roaming/Typora/typora-user-images/image-20220822193852648.png)]

    • 出队
    image-20220822193909718 image-20220822193921468
    • 队满
    image-20220822193941900
    image-20220822193952526

    3.2.4双端队列

    image-20220822194018385 image-20220822194033781
    • 考点
    image-20220822194054159 image-20220822194121407

    输入受限

    image-20220822194159052 image-20220822194230700

    输出受限:

    image-20220822194252974 image-20220822194308352
    image-20220822194323344

    3.3 栈的应用

    3.3.1 括号匹配问题

    image-20220822194405718

    image-20220822194417790

    image-20220822194438368

    image-20220822194455094


    image-20220822194508735

    3.3.2 表达式求值

    image-20220822194528064

    三种算术表达式

    image-20220822194750772 image-20220822194908584 image-20220822194935985

    🐟中缀

    • 中缀表达式的计算(用栈实现)
    image-20220822203922111

    例:

    image-20220822204256105
    image-20220822204358691
    image-20220822204426036
    image-20220822204449435
    image-20220822204515966
    image-20220822204530265
    • 中缀转后缀(手算):
    image-20220822195431707 image-20220822195757229

    即:

    image-20220822201200654
    • 中缀转后缀(机算)
    image-20220822201233615

    如:

    image-20220822202632424
    image-20220822202707222

    -扫到除号时,乘号弹出;由于此时无法确定右侧式子中是否有括号,此时栈中的减号不弹出

    image-20220822203155801
    image-20220822203310416
    image-20220822203322702

    另一个带有括号的例子:

    image-20220822203413494
    image-20220822203510734
    image-20220822203659448
    image-20220822203741693
    • 中缀转前缀(手算):
    image-20220822200445820

    image-20220822200553643

    🐟后缀

    • 后缀表达式的计算(手算)
    image-20220822195931763 image-20220822200041256
    • 后缀表达式的计算(机算)
    image-20220822200116122 image-20220822200206389 image-20220822200229629

    🐟前缀

    image-20220822200747939

    image-20220822201018826

    image-20220822204557677

    3.3.3 递归

    image-20220822204711027 image-20220822204729387
    • Eg1:

    image-20220822204758662

    • Eg2:

    image-20220822204828883


    image-20220822204850811

    3.4 队列的应用

    • 树的层次遍历(见树章节)

    • 图的广度优先遍历(见图章节)

    • 在操作系统中的应用

    image-20220822205630684 image-20220822205704997

    3.5 数组和特殊矩阵

    image-20220822205739322

    3.5.1 数组的存储结构

    • 一维数组
    image-20220822210042320
    • 二维数组
    image-20220822210110476

    行优先存储:

    image-20220822210140612

    列优先存储:

    image-20220822210159938

    3.5.2 特殊矩阵的压缩存储

    • 普通矩阵
    image-20220822210650768

    对称矩阵

    image-20220822211211237 image-20220822211246024 image-20220822211332357 image-20220822211434412 image-20220822211511878

    出题方法:

    image-20220822211529437

    三角矩阵

    image-20220822212048292

    下三角:

    image-20220822212103162

    上三角:

    image-20220822212119866

    三对角矩阵

    image-20220822212358416

    image-20220822212415406 image-20220822224657248

    稀疏矩阵

    image-20220822224738274 image-20220822224914522

    image-20220822224934623

    image-20220822224949755
  • 相关阅读:
    基于 CNN-GRU 的菇房多点温湿度预测方法研究 学习记录
    Git-cherry pick的使用
    老卫带你学---leetcode刷题(89. 格雷编码)
    MySQL数据库基本操作-DQL-基本查询
    万字泣血解析割韭菜内情,程序员别老想着做副业
    h5头部返回栏代码-组件封装
    使用AOP做日志切面
    【C++修炼之路】8. string类详解
    前端如何使网页变灰
    leetcode 406. 根据身高重建队列
  • 原文地址:https://blog.csdn.net/weixin_48434658/article/details/126475263