栈的特点:后进先出
核心算法
#ifndef _STACK_H_
#define _STACK_H_
#define _STACK_H_
#define STACK_SIZE 100
#define INCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define INCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status DestoryStack(SqStack &S);
Status ClearStack(SqStack &S);
Status EmptyStack(SqStack &S);
Status Push(SqStack &S,ElemType e);
Status Pop(SqStack &S,ElemType &e);
Status Compare(SqStack &S);
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S);
Status DestoryStack(SqStack &S);
Status ClearStack(SqStack &S);
Status EmptyStack(SqStack &S);
Status Push(SqStack &S,ElemType e);
Status Pop(SqStack &S,ElemType &e);
Status Compare(SqStack &S);
#endif
#include"stack.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
//构造空栈
Status InitStack(SqStack &S)
{
S.base=(ElemType*)malloc(STACK_SIZE*sizeof(ElemType));
if(!S.base)
{
return OVERFLOW;
}
else
{
S.top=S.base;
S.stacksize=STACK_SIZE;
return OK;
}
}
Status InitStack(SqStack &S)
{
S.base=(ElemType*)malloc(STACK_SIZE*sizeof(ElemType));
if(!S.base)
{
return OVERFLOW;
}
else
{
S.top=S.base;
S.stacksize=STACK_SIZE;
return OK;
}
}
//销毁栈
Status DestoryStack(SqStack &S)
{
ElemType e;
if(S.top!=S.base)
{
Pop(S,e);
}
else
{
return OK;
}
}
//置空栈
Status ClearStack(SqStack &S)
{
if(S.top!=S.base)
{
S.top=S.base;
}
else
{
return OK;
}
}
//清空栈
Status EmptyStack(SqStack &S)
{
if(S.top==S.base)
{
return TRUE;
}
else
{
return ERROR;
}
}
//压栈
Status Push(SqStack &S,ElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(ElemType*)realloc(S.base,(STACK_SIZE+INCREMENT)*sizeof(ElemType));
if(!S.base)
{
return OVERFLOW;
}
else
{
S.top=S.base+S.stacksize;
S.stacksize+=INCREMENT;
}
}
else
{
*S.top++=e;
return OK;
}
}
//弹栈
Status Pop(SqStack &S,ElemType &e)
{
if(S.top==S.base)
{
return ERROR;
}
else
{
e=*--S.top;
return OK;
}
}
//括号匹配
Status Compare(SqStack &S)
{
ElemType e;
int flag=TRUE;
char ch;
while((ch=getchar())!='$'&&flag)//$为字符串输入的结束标志
{
switch(ch)
{
case '(':
case '[':
case'{':
Push(S,ch);
break;
case ')':
if(Pop(S,e)==ERROR||e!='(')
{
flag=FALSE;
}
break;
case ']':
if(Pop(S,e)==ERROR||e!='[')
{
flag=FALSE;
}
break;
case '}':
if(Pop(S,e)==ERROR||e!='{')
{
flag=FALSE;
}
break;
}
}
if(flag&&ch=='$'&&EmptyStack(S))
{
return TRUE;
}
else
{
return FALSE;
}
}
Status DestoryStack(SqStack &S)
{
ElemType e;
if(S.top!=S.base)
{
Pop(S,e);
}
else
{
return OK;
}
}
//置空栈
Status ClearStack(SqStack &S)
{
if(S.top!=S.base)
{
S.top=S.base;
}
else
{
return OK;
}
}
//清空栈
Status EmptyStack(SqStack &S)
{
if(S.top==S.base)
{
return TRUE;
}
else
{
return ERROR;
}
}
//压栈
Status Push(SqStack &S,ElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(ElemType*)realloc(S.base,(STACK_SIZE+INCREMENT)*sizeof(ElemType));
if(!S.base)
{
return OVERFLOW;
}
else
{
S.top=S.base+S.stacksize;
S.stacksize+=INCREMENT;
}
}
else
{
*S.top++=e;
return OK;
}
}
//弹栈
Status Pop(SqStack &S,ElemType &e)
{
if(S.top==S.base)
{
return ERROR;
}
else
{
e=*--S.top;
return OK;
}
}
//括号匹配
Status Compare(SqStack &S)
{
ElemType e;
int flag=TRUE;
char ch;
while((ch=getchar())!='$'&&flag)//$为字符串输入的结束标志
{
switch(ch)
{
case '(':
case '[':
case'{':
Push(S,ch);
break;
case ')':
if(Pop(S,e)==ERROR||e!='(')
{
flag=FALSE;
}
break;
case ']':
if(Pop(S,e)==ERROR||e!='[')
{
flag=FALSE;
}
break;
case '}':
if(Pop(S,e)==ERROR||e!='{')
{
flag=FALSE;
}
break;
}
}
if(flag&&ch=='$'&&EmptyStack(S))
{
return TRUE;
}
else
{
return FALSE;
}
}
#include"stack.h"
#include<iostream>
using namespace std;
SqStack S;
ElemType e;
#include<iostream>
using namespace std;
SqStack S;
ElemType e;
int main()
{
if(InitStack(S)==OVERFLOW)
{
cout<<"内存分配失败!"<<endl;
}
else
{
if(Compare(S)==TRUE)
{
cout<<"括号匹配成功!"<<endl;
}
else
{
cout<<"括号匹配不成功!"<<endl;
}
}
DestoryStack(S);
return 0;
}
{
if(InitStack(S)==OVERFLOW)
{
cout<<"内存分配失败!"<<endl;
}
else
{
if(Compare(S)==TRUE)
{
cout<<"括号匹配成功!"<<endl;
}
else
{
cout<<"括号匹配不成功!"<<endl;
}
}
DestoryStack(S);
return 0;
}