顺序表实现括号的配对
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 20
typedef char ElemType;
typedef int Status;
typedef struct
{
ElemType data[MAXSIZE];
int Length;
int count;
}SqList;
//顺序表的初始化
Status InitSqList(SqList * L)
{
L->Length=0;
L->count=0;
return OK;
}
//顺序表的建立
Status Create(SqList * L)
{
printf("请输入元素,以'#'结束:\n");
int i=0;
while(scanf("%c",&(L->data[i])),L->Length++,L->data[i]!='#')
{
i++;
}
return OK;
}
//括号的配对
Status Matches(SqList * L)
{
for(int i=0;i<L->Length-1;i++)
{
if(L->data[i]=='(')
{
L->count++;
}
if(L->data[i]==')')
{
L->count--;
}
if(L->count==-1)
{
break;
}
}
if(L->count==0)
{
printf("括号成功配对!\n");
return OK;
}
else
{
printf("括号配对失败!\n");
return ERROR;
}
}
int main()
{
SqList L;
InitSqList(&L);
Create(&L);
Matches(&L);
return OK;
}