#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
typedef struct
{
int stack_size;
int top;
char* ch;
}Stack;
int InitStack( Stack&s,char* t) //初始化顺序栈的大小
{ int length = strlen(t);
s.ch = (char*)malloc(length * sizeof(char)); //初始化了顺序栈的大小
s.stack_size = length;
if (s.ch != NULL) {
s.top = 0; //将顺序栈置空
}
return 0;
}
int Push(Stack& s, char e) //入栈
{ if (s.top == s.stack_size)// 判断栈是否上溢 return 1;
s.ch[s.top] = e;
s.top++;
return 0;}
char Pop(Stack& s) //出栈{
char temp;
if (s.top == 0) //判断栈是否下溢
return 1;
else {
s.top--;
temp = s.ch[s.top];
return temp;
}
}
int IsPlalindrome(char* t) //判断是否为回文{ Stack s; int len, i = 0;
InitStack(s, t); //先初始化栈
len = strlen(t); //求出字符串的长度 for ( i = 0; i < len / 2; i++)
Push(s, t[i]);
if (len % 2 == 1) i++; while (s.top >0) {
if (Pop(s) != t[i]) {
return 0;
}
i++;
}
return 1;
}
int main(){
char str[100];
printf("请输入一个字符序列:"); scanf("%s",str);
if(IsPlalindrome(str))
printf("该字符序列为回文序列!\n");
else
printf("该字符序列不是回文序列!\n"); return 0;
}