回文判断程序
#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACK_INCREMENT 10 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW 0 typedef char SelemType; typedef struct SqStack{ SelemType *base; SelemType *top; int stacksize; }SqStack; SelemType a[STACK_INIT_SIZE+STACK_INCREMENT]; void InitStack(SqStack *sq){ sq->base = (char *)malloc(STACK_INIT_SIZE*sizeof(char)); if(!sq->base){ exit(OVERFLOW); } sq->top=sq->base; sq->stacksize = STACK_INIT_SIZE; } int StackEmpty(SqStack sq){ if(sq.base == sq.top) return TRUE; else return FALSE; } int StackLength(SqStack sq){ return (sq.top-sq.base); } void Push(SqStack *sq,char e){ if(sq->top-sq->base == sq->stacksize){ sq->base = (char*)realloc(sq->base,(sq->stacksize+STACK_INCREMENT)*sizeof(char)); if(!sq->base) exit(OVERFLOW); sq->top = sq->base+STACK_INCREMENT; sq->stacksize +=STACK_INCREMENT; } *(sq->top++) = e; } int Pop(SqStack *s,char *e){ if(s->top == s->base ) return ERROR; *e = *--s->top; return OK; } int IsReverse(SqStack s){ int j; char b; for(j=0;j<s.stacksize;j++){ if(Pop(&s,&b)==0) break; if(b!=a[j]) return 0; } return 1; } int main(){ SqStack q; int i=0; InitStack(&q); char m; printf("请输入数据:"); scanf("%c",&m); while(m!='@'){ Push(&q,m); a[i]= m; i++; scanf("%c",&m); } if(StackEmpty(q)==1){ printf("此为空栈!\n"); exit(1); } int flag; flag = IsReverse(q); if(flag==0) printf("%s不是回文序列\n",a); else printf("%s是回文序列\n",a); return 0; }