王道计算机考研 数据结构C语言复现-第五章-栈

本文分享了王道考研课程中关于数据结构的代码示例,包括顺序栈和共享栈的实现,以及用于检查括号匹配的递归函数。作者旨在全面复习并帮助读者共同进步。
摘要由CSDN通过智能技术生成

这篇文章收录了王道考研课程中涉及的数据结构的所有代码。此外,本博客可能会添加一些额外的代码(不仅限于王道考研),因为408考试中会频繁考察一些冷门的知识点,所以这篇博客会涵盖所有相关的代码。这也是我数据结构的第一轮复习,希望能与大家共同进步。由于博客篇幅的限制,可能无法一次性包含所有内容,欢迎指出代码错误部分!!!

你想要的都在下面!!!

// @FileName  :ZhanandDuiLie.c
// @Time      :2023/8/14 20:09
// @Author    :YKW
#define MaxSize 10
# include <stdio.h>
# include <stdbool.h>
//栈(Stack)是只允许在一段进行插入或删除操作的线性表
//后进先出
//顺序栈
typedef struct{
    int data[MaxSize];//栈内元素
    int top;//栈顶指针,此处是下标
}SqStack;
void InitStack(SqStack *S){
    S->top=-1;
}
//新元素入栈
bool Push(SqStack *S,int x){
    if(S->top==MaxSize-1)//栈满了
        return false;
    S->top=S->top+1;//
    S->data[S->top]=x;
    return true;
}
bool Pop(SqStack *S,int x){
    if(S->top==-1)//栈空
        return false;
    x=S->data[S->top--];
    return true;
}
bool GetTop(SqStack S,int *x){
    if(S.top==-1)
        return false;
    *x=S.data[S.top];//
    return true;
}
bool bracketCheck(char str[],int length){
    SqStack S;
    InitStack(&S);
    for(int i=0;i<length;i++){
        if(str[i]=='('||str[i]=='['||str[i]=='{')
            Push(&S,str[i]);
        else{//右括号
            if(S.top==-1)//栈空&右括号
                return false;
            char topElem;
            Pop(&S,topElem);
            if(str[i]==')'&&topElem!='(')
                return false;
            if(str[i]==']'&&topElem!='[')
                return false;
            if(str[i]=='}'&&topElem!='{')
                return false;
        }
    }
    return S.top==-1;
}

//共享栈
typedef struct{
    int data[MaxSize];
    int top1;
    int top2;
}ShStack;

//链栈
typedef struct Linknode{
    int data;
    struct Linknode *next;
} *LiStack;
int main(){

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值