#include < iostream >
#define maxSize 100
using namespace std;
// 定义栈
typedef struct{
char data[maxSize];
int top;
}Stack;
// 初始化栈
void initStack(Stack &S) // !!要“& ”
{
S.top = -1;
}
// 判空
int empty(Stack S){
if(S.top == -1)
return 1;
return 0;
}
// 进栈
void push(Stack &S, char e)
{
if(S.top == maxSize-1){
cout<<"The stack was full!"<<endl;
return;
}
S.data[++S.top] = e;
}
// 出栈
void pop(Stack &S, char &e)
{
if(empty(S)){
cout<<"The stack was empty!"<<endl;
return;
}
e = S.data[S.top--];
}
// 判断回文函数
int huiwen(char a[])
{
char e;
Stack S; // 在回文的函数里建栈
initStack(S);
for(int i=0;a[i]!='\0';i++){
push(S,a[i]);
}
for(int i=0;a[i]!='\0';i++) {
pop(S,e);
if(e!=a[i]) // 数值不一样
return 0;
}
return 1;
}
int main()
{
char a[] = "12321"; // 不定义数组的大小
if(huiwen(a))
cout<<a<<"是回文数。"<<endl;
else
cout<<a<<"不是回文数。"<<endl;
return 0;
}