利用顺序栈判断字符串是否回文
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; }
运行结果:
相关文章推荐
- 数据结构常见问题(二)利用栈和队列判断字符串是否是回文
- 利用栈和队列判断字符串是否是回文——c语言
- C语言利用栈判断字符串是否为回文
- 判断一个字符串是否为回文的递归算法
- 写一函数int fun(char *p)判断一字符串是否为回文,是返回1,不是返回0,出错返回-1
- 利用strpos判断字符串是否存在指定字符串
- 利用正则表达式判断一个字符串是否为数字
- 判断一个字符串是否为回文的递归算法
- asp.net中利用正则表达式判断一个字符串是否为数字的代码
- C#--第2周实验--任务7--编写一个控制台应用--判断S所指的字符串是否回文(即顺读和逆读相同的字符串)
- 妙趣横生算法 4:判断字符串是否回文
- 判断一个字符串是否回文?
- 判断一个字符串是否为回文的非递归算法
- 判断字符串是否可以通过交换相邻字符得到回文字符串,如果可以输出最少交换次数
- 第二周 任务七 判断S所指的字符串是否回文
- HDU2163_判断字符串是否为回文_速度较慢
- 每天一道算法题1 判断字符串是否是回文字符串或者是否含有回文字符子串
- 判断字符串是否回文
- 判断是否为回文字符串
- 编写函数,判断一个字符串是否是回文