#include <stdlib.h>
#include <stdio.h>
#define MaxSize 50
#define true 1
#define false 0
typedef struct
{
char data[MaxSize];
int top;
}SqStack;
void IninStack(SqStack *S)
{
S->top=-1;
}
_Bool StackEmpty(SqStack *S)
{
if(S->top==-1)
return true;
else
return false;
}
_Bool Push(SqStack *S,char x)
{
if(S->top==MaxSize-1)
return false;
S->data[++S->top]= x;
printf("%c入栈\n",x);
return true;
}
_Bool Pop(SqStack *S,char * x)
{
if(S->top==-1)
return false;
*x=S->data[S->top];
S->top--;
return true;
}
_Bool Top(SqStack *S,char * x)
{
if(S->top==-1)
return false;
*x=S->data[S->top];
return true;
}
_Bool ManageTrain(char str[],int n,SqStack *S)
{
int i=0;
while(i<n)
{
switch (str[i])
{
case 'H':
if(str[i]=='H')
printf("第%d号%c输出\n",i+1,str[i]);
break;
case 'S':
if(str[i]=='S')
if(!Push(S,str[i])) return false;
break;
default:
break;
}
i++;
}
while(!StackEmpty(S))
{
char temp;
Pop(S,&temp);
printf("%c出栈;;\n",temp);
printf("%c输出\n",temp);
}
}
int main()
{
SqStack S;
IninStack(&S);
char str[7]={'H','S','H','S','H','S','H'};
ManageTrain(str,7,&S);
return 0;
}
实现HS火车按序输出
最新推荐文章于 2022-08-30 20:16:22 发布