mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
🏷️ Add QueueApplication
This commit is contained in:
@@ -11,6 +11,7 @@ typedef struct {
|
||||
int top;
|
||||
} SqStack;
|
||||
|
||||
|
||||
//初始化
|
||||
void InitStack(SqStack &S) {
|
||||
S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
|
||||
122
DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp
Normal file
122
DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// Created by kim yang on 2020/8/3.
|
||||
//
|
||||
|
||||
//栈的应用
|
||||
|
||||
/**以下都是基础操作定义以及实现的方式和前面一样**/
|
||||
#include <stdio.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
char data[MaxSize];
|
||||
int top;
|
||||
} SqStack;
|
||||
|
||||
//初始化
|
||||
void InitStack(SqStack &S) {
|
||||
S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
}
|
||||
|
||||
//入栈
|
||||
bool Push(SqStack &S, char t) {
|
||||
if (S.top == MaxSize - 1)return false;//栈满
|
||||
S.data[++S.top] = t;
|
||||
// 等价下面两个语句
|
||||
// S.top+=1;//先将栈顶指针指向下一个位置
|
||||
// S.data[S.top]=t;//再填充元素
|
||||
return true;
|
||||
}
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop(SqStack &S, char &x) {
|
||||
//判断
|
||||
if (S.top == -1)return false;//栈空报错
|
||||
x = S.data[S.top--];
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//读取栈顶元素
|
||||
bool StackEmpty (SqStack S) {
|
||||
return S.top==-1;
|
||||
}
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top >= 0) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
/**以上都是基础操作定义以及实现的方式和前面一样**/
|
||||
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 (StackEmpty(S))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 StackEmpty(S);//最后检查栈,若空匹配成功,非空匹配失败
|
||||
}
|
||||
|
||||
|
||||
void TestQueueApplication(){
|
||||
printf("开始测试\n");
|
||||
|
||||
/**基础操作测试**/
|
||||
|
||||
SqStack S;
|
||||
printf("测试第一种初始化方式\n");
|
||||
InitStack(S);
|
||||
if (Push(S, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push(S, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
char x;
|
||||
if (Pop(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
|
||||
/**测试括号问题部分源码**/
|
||||
if(bracketCheck("[({})]",6))
|
||||
printf("字符串括号匹配成功\n");
|
||||
else
|
||||
printf("字符串括号匹配失败\n");
|
||||
if(bracketCheck("[({})",5))
|
||||
printf("字符串括号匹配成功\n");
|
||||
else
|
||||
printf("字符串括号匹配失败\n");
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
int main(){
|
||||
TestQueueApplication();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user