关键词:
本周老师作业留了两个。先上传一个吧。那个有时间我再传上来~
本周的要求:
1.给出顺序栈的存储结构定义。
2.完成顺序栈的基本操作函数。
1) 初始化顺序栈
2) 实现入栈和出栈操作
3) 实现取栈顶元素和判空操作
括号匹配问题
3.编写主函数实现基本操作函数功能,并设置测试数据,测试合法和非法数据的输出结果。
4.程序调试运行并保存输出结果。
5.整理并提交实验作业。
1 #include <cstdio> 2 #include <cstring> 3 #define Stack_Size 50 4 5 typedef struct 6 { 7 char a[Stack_Size]; 8 int top; 9 }SeqStack; 10 11 int IsEmpty(SeqStack *S)//栈判空 12 { 13 return S->top == -1; 14 } 15 16 int Match(char ch1,char ch2)//字符串匹配 17 { 18 switch(ch2){ 19 case ‘)‘: 20 if(ch1==‘(‘) 21 return 1; 22 break; 23 case ‘}‘: 24 if(ch1==‘{‘) 25 return 1; 26 break; 27 case ‘]‘: 28 if(ch1==‘[‘) 29 return 1; 30 break; 31 } 32 return 0; 33 } 34 35 void InitStack(SeqStack * S)//初始化顺序栈 36 { 37 S->top = -1; 38 } 39 40 void Push(SeqStack * S,char x)//进栈 41 { 42 S->top++; 43 S->a[S->top]=x; 44 } 45 46 void Pop(SeqStack * S,char *x)//出栈 47 { 48 *x=S->a[S->top]; 49 S->top--; 50 } 51 52 void GetTop(SeqStack * S,char *x) 53 { 54 *x=S->a[S->top]; 55 } 56 57 58 void BracketMatch(char *str) 59 { 60 SeqStack S; 61 char ch; 62 InitStack(&S); 63 for(int i=0;str[i]!=‘