#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 发布
这篇博客介绍了一个使用C语言实现的顺序栈,包括初始化、判断栈空、入栈、出栈和查看栈顶元素等操作。此外,还展示了如何结合字符串处理,对特定字符进行管理和输出。示例代码中,对于字符串'HSHSHSHS',将'S'字符入栈,然后依次出栈并输出,实现了字符的特定序列处理。
4406

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



