数据结构C语言用栈判断回文数

#include
#include
#include
#define MAXSIZE 100
typedef char DataType;
typedef struct
{
DataType data[MAXSIZE];
int top;
}SqStack;
int InitStack(SqStack *S)//初始化栈
{
S->top=-1;
return 1;
}
int StackEmpty(SqStack *S)//判栈空
{
return(S->top==-1?1:0);
}
int StackFull(SqStack *S)//判栈满
{
return(S->top==MAXSIZE-1?1:0);
}
int Push(SqStack *S,DataType e)//进栈
{
if(StackFull(S))
return 0;
S->top++;
S->data[S->top]=e;
return 1;
}
int Pop(SqStack *S,DataType e)//出栈
{
if(StackEmpty(S))
return 0;
e=S->data[S->top];
S->top--;
return 1;
}
void Judgment(SqStack *S)//判断是否为回文数
{
int i=0,j,next,len,mid;
DataType x;
char a[MAXSIZE];
InitStack(S);
printf("请输入要判断的字符串,用回车结束字符的输入\n");
scanf("%s",a);
len=strlen(a);//取字符串a的长度
mid=len/2;
for(i=0;i{
Push(S,a[i]);
}
if(len%2==0)
{
next=mid;
}
else
{
next=mid+1;
}
for(i=next;i<=len-1;i++)
{
if(a[i]==S->data[S->top])
{
Pop(S,x);
}
else break;
}
if(StackEmpty(S))
{
printf("该字符串是回文数\n");
}
else
{
printf("该字符串不是回文数呀!\n");
}
}
int main()
{
SqStack *S;
S=(SqStack*)malloc(sizeof(SqStack));
InitStack(S);
printf(" 用栈判断字符串是否为回文数\n");
Judgment(S);
}

相关文档
最新文档