此代码可以正常运行,是是实实在在的类C语言
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef char ElemType;
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
enum Status{ERROR,OK};
//初始化栈
Status InitStack(SqStack &s)
{
s.top=-1;
return OK;
}
//入栈
Status Push(SqStack &s)
{
ElemType x;
while(x!='\n')
{
scanf("%c",&x);
s.data[++s.top]=x;
};
return OK;
}
//判断合法性
int JudgeLegal(SqStack &s)
{
int i=0;
int j=0;
int k=0;
while(s.data[i]!='\0')
{
switch(s.data[i])
{
case 'I':j++;break;
case 'O':k++;if(k>j){return -1;}break;
}
i++;
}
if(j!=k)
{
return -1;
}
else
{
return 0;
}
}
int main(int argc,char* argv[])
{
SqStack S;
InitStack(S);
Push(S);
if(JudgeLegal(S))
{
printf("序列不合法\n");
}
else
{
printf("序列合法!\n");
}
return 0;
}
本文探讨如何使用C语言判断一个由I和O组成的序列是否表示合法的栈操作。通过代码实现,解析栈的初态和终态为空时,入栈和出栈操作序列的合法性。

3115

被折叠的 条评论
为什么被折叠?



