题目:借助栈 把字符数组train中的字符S都调到字符H的前面
示例:“HSHSHSHSHS”-->"SSSSSHHHHH"
出错描述:编译器显示 弹出“停止工作”窗口
# include<stdio.h>
# define n 20
# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
typedef int status;
typedef char ElemType;
typedef struct
{
ElemType data[n];
int top;
}SqStack;
void Init_Sq(SqStack *S)
{
S->top=-1;
}
status Push_Sq(SqStack *S,ElemType e)
{
if(S->top+1==n)
return ERROR;
S->data[++S->top]=e;
return OK;
}
status Pop_Sq(SqStack *S,ElemType *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top--];
return OK;
}
status EmptyStack_Sq(SqStack S)
{
if(S.top==-1)
return TRUE;
else
return FALSE;
}
status Train_Arrange(char *train)
{
char *p=train,*q=train,e;
SqStack S;
Init_Sq(&S);
while(*p)
{//HSHSHSHSHS
if(*p=='H')
{
Push_Sq(&S,*p);
printf("p==%p\n",*p);
}
else
*(q++)=*p;
p++;
}
while(EmptyStack_Sq(S)==FALSE)
{
Pop_Sq(&S,&e);
*(q++)=e;//这儿也有问题,但是不清楚原因
}
return OK;
}
void Traverse_Sq(char *train)
{
ElemType *p;
p=train;
while(*p!='\0')
printf("%c",*p++);
printf("\n");
}
int main()
{
char *train="HSHSHSHSHS";
Train_Arrange(train);
printf("转换后的火车序列为:");
Traverse_Sq(train);
return 0;
}