参考书:数据结构教程 第5版 李葆春 P80
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*顺序栈定义*/
#define MaxSize 100
typedef struct
{
char data[MaxSize];
int top;
}SqStack;
int j;
/*初始化栈*/
void InitStack(SqStack *s){
s->top=-1;
s=(SqStack *)malloc(sizeof(SqStack));
}
/*销栈*/
void DestroyStack(SqStack *s){
free(s);
}
/*判断栈是否为空*/
int StackEmpty(SqStack *s){
return (s->top==-1);
}
/*进栈*/
int Push(SqStack *s, int e){
if(s->top==MaxSize-1)
{
return 0;
}
s->top++;
s->data[s->top]=e;
}
/*出栈*/
int Pop(SqStack *s, int *e){
if(s->top==-1){
return 0;
}
*e=s->data[s->top];
s->top--;
}
/*取栈顶元素*/
int GetTop(SqStack *s,int *e){
*e=s->data[s->top];
if(s->top==-1){
return 0;
}
}
/*判断字符串是否为对称字符串*/
void symmetry(char str[]){
int i;
char e;
SqStack st;
InitStack(&st);
for(i=0; str[i]!='\0'; i++){
Push(&st, str[i]);
}
printf("%s\n",st.data);
for(j=0; str[j]!='\0'; j++){
printf("%c\n",str[j]);
Pop(&st, &e);
printf("%c-%c\n",e,str[j]);
if(str[j]!=e){
printf("非对称字符串\n");
DestroyStack(&st);
break;
}
}
if (str[j]=='\0')
{
printf("字符串为对称字符串\n");
DestroyStack(&st);
}
}
int main(){
char str[10]={"abczcba"};
symmetry(str);
}