diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp index f792a77..a787022 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp @@ -11,6 +11,7 @@ typedef struct { int top; } SqStack; + //初始化 void InitStack(SqStack &S) { S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素 diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp new file mode 100644 index 0000000..e00fc54 --- /dev/null +++ b/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp @@ -0,0 +1,122 @@ +// +// Created by kim yang on 2020/8/3. +// + +//栈的应用 + +/**以下都是基础操作定义以及实现的方式和前面一样**/ +#include + +# 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