# palindrome

Algorithm 专栏收录该内容
24 篇文章 0 订阅

A string that reads the same thing as the inverse reads (excluding whitespace) is called palindrome.

Design algorithm to determine whether a string is palindrome,

requires the use of stack to achieve discrimination of palindrome.

#include <iostream>
#include <cstring>
using namespace std;
#define MaxSize 1000
class SqStack
{
public:
SqStack();
bool isEmpty();
void push(char ch);
char pop();
void Print();
void Palindrome();
private:
char data[MaxSize];
int top;
};
SqStack::SqStack()
{
this->top=-1;
}
bool SqStack::isEmpty()
{
if(this->top=-1)
return true;
else
return false;
}
void SqStack::push(char ch)
{
if(top == MaxSize-1)
cout<<"栈满"<<endl;
else
{
++top;
data[top] = ch;
}
}
char SqStack::pop()
{
char ch;
if(top==-1)
{
cout<<"栈空"<<endl;
return 0;
}
else
{
ch = data[top];
--top;
}
return ch;
}
void SqStack::Palindrome()
{
bool tag = true;
int n = top;
for(int i =0;i<(top+1)/2;++i)
{
if(data[i]!=data[n])
{
tag = false;
break;
}
n--;
}
if(tag)
cout<<"回文数"<<endl;
else
cout<<"非回文数"<<endl;
}
void SqStack::Print()
{
if(top==-1)
cout<<"栈空"<<endl;
else
cout<<data<<endl;
}
int main()
{
SqStack sq1,sq2;
string s;
cin>>s;
char q;
cout<<"字符串长度："<<s.length()<<endl;
for(int i= 0;i<s.length()/2;++i)
{
sq1.push(s[i]);
sq2.push(s[s.length()-1-i]);
}
cout<<"字符串为："<<s<<endl;
int tag=true;
for(int i= 0;i<s.length()/2;++i)
{
if(sq1.pop()!=sq2.pop())
{
tag = false;
break;
}
}
if(tag)
cout<<s<<"是回文数"<<endl;
else
cout<<s<<"不是回文数"<<endl;
//sq.Palindrome();
//sq.pop(q);//出栈

return 0;
}


• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

08-10 254
07-16 906

04-02 390
08-02 247
04-26 750
07-31
03-13 225
07-11 273
01-02 104