#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FULL 10000
#define MAX 10000
typedef struct elem {
char d;
struct elem *next;
}elem;
typedef struct stack { //定义栈
int cnt;
struct elem *top;
}stack;
void push(char d,struct stack *stk){
struct elem *p;
if (stk->cnt != FULL){
p = (struct elem *)malloc(sizeof(struct elem));
p->d = d;
p->next = stk->top;
stk->top = p;
stk->cnt++;
}
}
char pop(struct stack *stk){
char d;
struct elem *p;
if(stk->cnt != FULL){
d = stk->top->d;
p = stk->top;
stk->top = stk->top->next;
stk->cnt--;
free(p);
}
return d;
}
void initialize(struct stack *stk){
stk->cnt = 0;
stk->top = NULL;
}
main(){
char input[MAX];
struct stack temp;
int i = 0;
int flag = 0;
initialize(&temp);
printf("请输入字符(以‘@’为结束符)\n");
scanf("%s", &input); //输入字符串
while (input[i] != '@'){ //字符串入栈
push(input[i],&temp);
i++;
}
while (temp.cnt != 0){ //字符依次出栈和字符数组比较,判断是否回文数
if (temp.top->d == input[flag]){
pop(&temp); //删除字符
flag++;
}
else{
printf("此字符序列不是回文序列!\n");
break;
}
}
if (temp.cnt == 0)
printf("此字符序列是回文序列!\n");
return 1;
}