#include<stdio.h>
#define MAX 100
typedef int datatype;
typedef struct
{
datatype a[MAX];
int top;
}sequence_stack;
void init(sequence_stack *L)
{
L->top=0;
}
int empty(sequence_stack L)
{
return(L.top?0:1);
}
datatype get(sequence_stack L) // 取得栈顶的值
{
if(L.top==0)
{
printf("栈是空的!\n");
return;
}
return L.a[L.top-1];
}
void push(sequence_stack *L,datatype x)
{
if(L->top==MAX)
{
printf("栈是满的!\n");
return;
}
L->a[L->top]=x;
L->top++;
}
void pop(sequence_stack *L)
{
if(L->top==0)
{
printf("栈的是空的!\n");
return;
}
L->top--;
}
int match_kuohao(char c[])
{
int i=0;
sequence_stack R;
init(&R);
while(c[i]!='#')
{
switch(c[i])
{
case '{': push(&R,c[i]);break;
case '[': push(&R,c[i]);break;
case '(': push(&R,c[i]);break;
case '}':
{
if(!empty(R)&&get(R)=='{')
{
pop(&R);
break;
}
else
return 0;
}
case ']':
if(!empty(R)&&get(R)=='[')
{
pop(&R);
break;
}
else
return 0;
case ')':
if(!empty(R)&&get(R)=='(')
{
pop(&R);
break;
}
else
return 0;
}
i++;
}
return (empty(R));
}
int main()
{
sequence_stack L;
datatype x;
char c[7];
int a=1,n;
while(a)
{
printf("\n\t\t 顺序表展示\n");
printf("**********************************************\n");
printf("* 1:初始化 2:判断是否为空 *\n");
printf("* 3:插入一个数 4:得到栈顶的数据 *\n");
printf("* 5:删除 6:匹配括号 *\n");
printf("* 0:退出 *\n");
printf("***********************************************\n");
//**********************************************
//* 1:初始化; 2:判断是否为空 *
//* 3:在末尾插入一个数 4:在指定的位置插入 *
//* 5:得到制定的数据 6:输出顺序表 *
//* 7:删除指定位置数据 0:退出 *
//***********************************************
scanf("%d",&a);
switch(a)
{
case 1: init(&L);printf("操作完成!请继续!\n\n\n");break;
case 2:
{
n=empty(L);
if(n==1)
{
printf("栈是空的!\n\n\n");
printf("操作完成!请继续!\n\n\n");
break;
}
else
{
printf("栈不是空的!\n\n\n");
printf("操作完成!请继续!\n\n\n");
break;
}
break;
}
case 3:
{
printf("请输入插入的数:");
scanf("%d",&x);
push(&L,x);
printf("操作完成!请继续!\n\n\n");
break;
}
case 4:
{
printf("栈顶的数据是:%d。\n",get(L));
printf("操作完成!请继续!\n\n\n");
break;
}
case 5:
{
pop(&L);
printf("操作完成!请继续!\n\n\n");
break;
}
case 6:
{
printf("请输入三对括号,以#结尾:");
scanf("%s",c);
n=match_kuohao(c);
if(n==1)
{
printf("匹配!\n");
break;
}
else
{
printf("不匹配!\n");
break;
}
}
case 0: printf("本次测试结束!\n");
}
printf("---------------------------------------------------\n\n");
}
}
运行图: