#include<iostream>
#include<malloc.h>
using namespace std;
typedef char ElemType;
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if (s->top==MaxSize-1) //栈满的情况,即栈上溢出
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if (s->top==-1) //栈为空的情况,即栈下溢出
return false;
e=s->data[s->top];
return true;
}
bool AllBrackets_Test(char ch[])
{
SqStack *s;
InitStack(s);
char e;
for(int i=0;ch[i]!='\0';i++)
{
if(ch[i]=='{'||ch[i]=='['||ch[i]=='(')
Push(s,ch[i]);
else if(ch[i]==']'||ch[i]=='}'||ch[i]==')')
{
if(StackEmpty(s))return false;
Pop(s,e);
if(ch[i]=='{'&&e!='}')return false;
if(ch[i]=='['&&e!=']')return false;
if(ch[i]=='('&&e!=')')return false;
}
}
if(!StackEmpty(s))
return false;
return true;
}
int main()
{
char ch[20]="{({[]})}";
cout<<AllBrackets_Test(ch);
return 0;
}
数据结构(判断三种括号是否配对 顺序栈 )
最新推荐文章于 2023-02-27 17:36:38 发布