2021-01-22

回文是指正读反读均相同的字符序列,如“abba"和"abdba"均是回文,但”good"不是回文。试写一个算法判定给定的字符序列是否为回文。(附完整可运行代码)

#include<stdio.h> 
#include<malloc.h>
#include<stdlib.h>
 #include<string.h>
 typedef int Status; 
typedef char SElemType;
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
 struct SqStack
{
 SElemType *top;
 SElemType *base;
 int stacksize;
};
void InitStack(SqStack &S)
{
 S.base=(SElemType*)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
 if(!S.base)
  exit(-1);
 S.top=S.base;
 S.stacksize=STACK_INIT_SIZE;
}
Status StackEmpty(SqStack S)
{
 if(S.top==S.base)
  return 1;
 else
  return 0;
}
void Push(SqStack &S,SElemType e)
{
 if(S.top-S.base==S.stacksize)
 {
  S.base=(SElemType*)realloc(S.base,(S.stacksize+STACK_INCREMENT)*sizeof(SElemType));
  if(!S.base)
   exit(-1); 
 S.top=S.base+S.stacksize;
 S.stacksize+=STACK_INCREMENT;
 }
 *S.top=e;
 S.top++;
}
Status Pop(SqStack &S,SElemType &e)
{
 if(S.top-S.base==0)
  return 0;
 e=*--S.top;
 return 1;
 } 
int HuiWen(char *str)  
{  
    SqStack S;  
    InitStack(S);  
    SElemType e;
    int len = strlen(str);  
    int i,judge=1;  
    for(i = 0;i<len/2;i++)  
    {  
       Push(S,*str);  
        *str++;  
    }  
    if(len%2 == 1) 
  *str++;
    for(int j=i;j>0;j--)  
    {  
        Pop(S,e);  
        if(*str == e)  
            *str++;  
        else  
            judge = 0;  
    }  
    return judge;  
}  
int main()
{
 SqStack S;
 InitStack(S);
 char t[]="abba";
 printf("该字符序列是否是回文序列? %d(1:是 0:否) ",HuiWen(t));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值