ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

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

2026/9/21 20:15:51 拓冰建站 浏览量
王道计算机考研 数据结构C语言复现-第五章-栈

这篇文章收录了王道考研课程中涉及的数据结构的所有代码。此外,本博客可能会添加一些额外的代码(不仅限于王道考研),因为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(){}