3.2回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符向量是否为回文。(提示:将一半字符入栈)

#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#define InitSize 10
typedef struct
{
    char *base;
    int top;
} SeqStack;

void PrintStack(SeqStack s);
const char *InitStack(SeqStack &S)
{
    S.base = new char[InitSize];
    S.top = -1;
    return "成功";
}
bool IsEmptyStack(SeqStack s)
{
    return s.top == -1;
}
bool Push(SeqStack &s, char e)
{
    if (s.top == InitSize - 1)
    {
        return false;
    }
    s.base[++s.top] = e;
    return true;
}
bool PopStack(SeqStack &s, char &e)
{
    if (s.top == -1)
    {
        return false;
    }
    e = s.base[s.top--];
    return true;
}
bool IsHuiWen(SeqStack &s, char *t)
{
    InitStack(s);
    int len = strlen(t);
    int i;
    for (i = 0; i < len / 2; i++)
    {
        Push(s, t[i]);
    }
    if (len % 2 != 0)
    {
        i++;
    }
    while (!IsEmptyStack(s))
    {
        char temp;
        PopStack(s, temp);
        if (temp != t[i])
        {
            return false;
        }
        else
            i++;
    }
    return true;
}
void PrintStack(SeqStack s)
{
    for (int i = 0; i <= s.top; i++)
    {
        printf("%c", s.base[i]);
    }
}
int main()
{
    SeqStack S;
    char t[] = "abdefdba";
    if (IsHuiWen(S, t))
    {
        printf("是回文!");
    }
    else
        printf("不是回文!");
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值