您的位置:首页 > 其它

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

2014-09-16 09:15 423 查看
/*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 9 月 16 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*问题描述:栈的基本操作
*程序输入:字符串
*程序输出:字符串是否回文
*问题分析:
*算法设计:
*/
#include <iostream>
#include <cstdlib>
#include <string>
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<<"请输入一个字符串:"<<endl;
cin>>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<<str<<"是回文字符串。"<<endl;
else
cout<<str<<"不是回文字符串。"<<endl;
return 0;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  回文字符串