From a24ddc57258f04784aa76dad52ab7afeca1d7f6f Mon Sep 17 00:00:00 2001 From: pp <3278045090@qq.com> Date: Fri, 27 Oct 2023 07:24:23 +0000 Subject: [PATCH] =?UTF-8?q?add=202224020147/chapter3/exp.3=5F1.cpp.=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=A1=BA=E5=BA=8F=E6=A0=88=E7=9A=84=E5=90=84?= =?UTF-8?q?=E7=A7=8D=E5=9F=BA=E6=9C=AC=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: pp <3278045090@qq.com> --- 2224020147/chapter3/exp.3_1.cpp | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 2224020147/chapter3/exp.3_1.cpp diff --git a/2224020147/chapter3/exp.3_1.cpp b/2224020147/chapter3/exp.3_1.cpp new file mode 100644 index 00000000..7e581b20 --- /dev/null +++ b/2224020147/chapter3/exp.3_1.cpp @@ -0,0 +1,77 @@ +#include +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct +{ + ElemType a[MaxSize]; + int top; +}Sqstack; +void initStack(Sqstack *& s) +{ + s=(Sqstack *)malloc(sizeof(Sqstack)); + s->top=-1; +} +void DestroyStack(Sqstack *& s) +{ + free(s); +} +bool JudgeSqstack(Sqstack *& s) +{ + return (s->top==-1); +} +bool Push(Sqstack *& s,ElemType e) +{ + if (s->==Maxsize-1) + { + return false; + } + s->top++; + s->a[s->top]=e; + return true; +} +bool Pop(Sqstack *& s,ElemType &e) +{ + if (s->top==-1) + { + return false; + } + e=s->a[s->top]; + s->top--; + return true; +} +bool GetTop(Sqstack *& s,ElemType &e) +{ + if (s->top==-1) + { + return false; + e=s->a[s->top]; + return true; +} +int main() +{ + Sqstack * s; + ElemType e; + printf("顺序栈的基本算法如下:"); + printf("1.初始化栈s:") + initStack(Sqstack *& s); + printf("2.判断栈是否为空:空为1非空为0"); + printf(JudgeSqstack(Sqstack *& s)); + printf("3.依次进栈元素a,b,c,d,e\n"); + Push(s,'a'); + Push(s,'b'); + Push(s,'c'); + Push(s,'d'); + Push(s,'e'); + printf("4.出栈序列为:e,d,c,b,a\n"); + while (!JudgeSqstack(s)) + { + pop(s,e); + printf("%c",e); + } + printf("5.栈为%s\n",(JudgeSqstack(s)?"空":"非空")) + printf("6.销毁栈") + DestroyStack(s)); + return 1; +} + \ No newline at end of file -- Gitee