请用C语言给出的顺序栈(栈的顺序储存结构)的类型定义

2024-11-22 18:42:28
推荐回答(1个)
回答(1):

#include
#include
int IsEmpty(int p)/*判断是否为空*/
{
if(p==0)
return 1;
else
return 0;
}
int Push(int p)/*入栈*/
{
p++;
return(p);
}
int Pop(int p)/*出栈*/
{
p--;
return(p);
}
void create()
{
char expression[20];
int i=0,p=0;
printf("please scan your expression which length less than 20,scan # to stop:\n");
scanf("%s",expression);
while(expression[i]!='#')
{
if(expression[i]=='(')/*如果遇到'('入栈*/
p=Push(p);
if(expression[i]==')')/*如果遇到')'且栈不为空'('出栈*/
{
if(IsEmpty(p))
{
printf("ERROR!\n");
exit(0);
}
else
p=Pop(p);
}
i++;
}
if(IsEmpty(p))
printf("OK!\n");
else
printf("ERROR!\n");
}
void main()
{
int i=0;
while(i!=-1)/*按-1退出程序运行*/
{
create();
printf("go on scan any number excepet for -1:\n");
scanf("%d",&i);
}
}