【无标题】栈的方法求字符串回文

#define _CRT_SECURE_NO_WARNINGS//调用C语言的语法

#include<iostream>

#include<string>

 

#define OK 1

#define ERROR 0

 

using namespace std;

 

typedef char ElemType;

typedef int status;

 

typedef struct stack {

 

 ElemType date;

 struct stack* next;

}stack,*SqStack;

 

//初始化一个栈

status init(SqStack& L) {

 

 L = new stack;

 L->next = NULL;

 return OK;

}

 

//进栈

status in(SqStack& L, ElemType e) {

 

 SqStack p;

 p = new stack;

 p->date = e;

 p->next = L;

 L = p;

 return OK;

}

 

ElemType Out(SqStack& L) {

 

 ElemType r;

 r = L->date;

 L = L->next;

 return r;

}

 

int main()

{

    while (1) {

        SqStack L;

 

        init(L);

 

        int length, result = 0;

        ElemType ch1, ch2;

 

        string str;

        cout << "请输入待判断字符串" << endl;

        cin >> str;

        const char* ch = str.data();

        length = strlen(ch);

 

        for (int k = 1;k <= length / 2;k++)

        {

            in(L, ch[k - 1]);

        }

 

        if (length % 2 == 0) //判断字符串长度是奇数还是偶数 

        {

            for (int i = 1;i <= length / 2;i++)

            {

                ch1 = Out(L);

                ch2 = ch[length / 2 + i - 1];

                if (ch1 == ch2) //进行判断 

                {

                    result = 1;

                    continue;

                }

                else

                {

                    break;

                }

            }

        }

        else

        {

            for (int j = 1;j <= length / 2;j++)

            {

                ch1 = Out(L);

                ch2 = ch[length / 2 + j];

                if (ch1 == ch2) //进行判断

                {

                    result = 1;

                    continue;

                }

                else

                {

                    break;

                }

            }

        }

        //输出操作 

        if (result)

        {

            cout << "该字符串是回文字符串!" << endl;

        }

        else

        {

            cout << "该字符串不是回文字符串!" << endl;

        }

    }

    return 0;

 

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值