利用顺序栈判断字符串是否回文

/*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 9 月 16 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*问题描述:栈的基本操作
*程序输入:字符串
*程序输出:字符串是否回文
*问题分析:
*算法设计:
*/
#include 
#include 
#include 
using namespace std;
const int MaxSize = 50;
struct SqStack
{
    char data[MaxSize];
    int top; //栈顶指针
};
void InitStack(SqStack *&s);//初始化栈
bool Push(SqStack *s,char e);//进栈
char Pop(SqStack *s);//出栈
void InitStack(SqStack *&s)//初始化栈
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}
bool Push(SqStack *s,char e)//进栈
{
    if(s->top==MaxSize-1)
        return false;
    else
    {
        s->top++;
        s->data[s->top]=e;
    }
    return true;
}
char Pop(SqStack *s)//出栈
{
    return (s->data[s->top--]);
}
int main()
{
    SqStack *s;
    bool flag = true;
    char str[MaxSize];
    InitStack(s);
    cout<<"请输入一个字符串:"<>str;
    for(int i = 0; str[i] != '\0'; i++)
        Push(s,str[i]);
    for(int j = 0; str[j] != '\0'; j++)
    {
        if(str[j]!=Pop(s))
        {
            flag = false;
            break;
        }
    }
    if(flag)
        cout<

运行结果:

利用顺序栈判断字符串是否回文_第1张图片

你可能感兴趣的:(【C++】,【数据结构】)