用堆栈解决括号匹配问题(C语言)
2013-11-13 13:05
483 查看
#include<stdio.h>
#include<stdlib.h>
int main()
{
void BracketMatch();
BracketMatch();
}
typedef char ElemType;
typedef struct Stack
{
int top;
int MaxSize;
ElemType *stack;
}Stack;
void Creat(Stack*s , int m)
{
if(m<0)
{
printf("%d is
negtive!",m);
return;
}
s->top=-1;
s->MaxSize=m;
s->stack=(ElemType*)malloc(sizeof(ElemType)*m);
}//Creat a stack
void Push(Stack *s, ElemType data)
{
if(s->top==s->MaxSize-1)
{
printf("The stack is
full!");
return;
}
s->top++;
s->stack[s->top]=data;
}//Push data in the stack s
ElemType Pop(Stack *s)
{
ElemType temp;
if(
s->top<-1)
{
printf("The stack is
NULL!");
return;
}
temp=s->stack[s->top];
s->top--;
return temp;
}//Pop the top element of the stack
void BracketMatch()
{
int n=20;
int i=0;
char str
;
char ch;
printf("请输入一个算术表达式(%d字符以内)\n",n);
scanf("%s",str);
Stack *s=(Stack*)malloc(sizeof(Stack));
Creat(s,n);//creat a stack of n elements
while(str[i]!='\0')
{
if(str[i]=='(' ||str[i]=='{'
||str[i]=='[' )
Push(s,str[i]);
if(str[i]==')')
{
ch=Pop(s);
if(ch!='(')
{
printf("Match
failure!\n");return; }
}
if(str[i]==']')
{
ch=Pop(s);
if(ch!='[')
{
printf("Match
failure!\n");return; }
}
if(str[i]=='}')
{
ch=Pop(s);
if(ch!='{')
{
printf("Match
failure!\n");return; }
}
i++;
}
if(s->top==-1) //最后堆栈有无元素判断
printf("Match
Successfully!\n");
else
printf("Match
failure!\n");
}
#include<stdlib.h>
int main()
{
void BracketMatch();
BracketMatch();
}
typedef char ElemType;
typedef struct Stack
{
int top;
int MaxSize;
ElemType *stack;
}Stack;
void Creat(Stack*s , int m)
{
if(m<0)
{
printf("%d is
negtive!",m);
return;
}
s->top=-1;
s->MaxSize=m;
s->stack=(ElemType*)malloc(sizeof(ElemType)*m);
}//Creat a stack
void Push(Stack *s, ElemType data)
{
if(s->top==s->MaxSize-1)
{
printf("The stack is
full!");
return;
}
s->top++;
s->stack[s->top]=data;
}//Push data in the stack s
ElemType Pop(Stack *s)
{
ElemType temp;
if(
s->top<-1)
{
printf("The stack is
NULL!");
return;
}
temp=s->stack[s->top];
s->top--;
return temp;
}//Pop the top element of the stack
void BracketMatch()
{
int n=20;
int i=0;
char str
;
char ch;
printf("请输入一个算术表达式(%d字符以内)\n",n);
scanf("%s",str);
Stack *s=(Stack*)malloc(sizeof(Stack));
Creat(s,n);//creat a stack of n elements
while(str[i]!='\0')
{
if(str[i]=='(' ||str[i]=='{'
||str[i]=='[' )
Push(s,str[i]);
if(str[i]==')')
{
ch=Pop(s);
if(ch!='(')
{
printf("Match
failure!\n");return; }
}
if(str[i]==']')
{
ch=Pop(s);
if(ch!='[')
{
printf("Match
failure!\n");return; }
}
if(str[i]=='}')
{
ch=Pop(s);
if(ch!='{')
{
printf("Match
failure!\n");return; }
}
i++;
}
if(s->top==-1) //最后堆栈有无元素判断
printf("Match
Successfully!\n");
else
printf("Match
failure!\n");
}
相关文章推荐
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
- 括号匹配问题 栈的应用 C语言实现
- 【面试题】用栈解决括号匹配问题
- 链栈的简单实现及括号匹配问题的链栈解决方法
- C语言使用深度优先搜索算法解决迷宫问题(堆栈)
- 用栈的链式存储结构来解决括号匹配问题
- 括号匹配问题(C++、堆栈)
- 数据结构 括号匹配问题 (栈 C语言)
- java堆栈类解决括号匹配问题
- C语言括号匹配问题
- 数据结构-C语言括号匹配问题(栈和计数方式两种思想)
- 题目1153:括号匹配问题(2010年北京大学计算机研究生机试真题,堆栈的使用)
- 堆栈应用——括号匹配问题
- c语言栈的应用之括号匹配问题
- 快速解决工作中遇到经典的括号匹配问题
- 数据结构中的栈,在解决很多问题都有用处,比如括号匹配,迷宫求解,表达式求值等等 java中有封装好的类,可以直接调用。
- 数据结构之括号匹配问题的解决
- 数据结构之SeqStack---堆栈应用---括号匹配问题
- 数据结构(八) 用顺序栈解决括号匹配的问题
- [C++]连续最长括号匹配问题解决策略二(学习)