利用栈判断回文,很简单的要求,可就是弄不出来啊
#include<stdio.h>#include<stdlib.h>
#define STACK 100
#define STACKINCREMENT 10
typedef struct{
char *base;
char *top;
int stacksize;
} sqstack;
int initstack(sqstack &s)
{ s.base=(char *)malloc(STACK *sizeof(char));
if(!s.base) exit(0);
s.top=s.base;
s.stacksize=STACK;
return 1;
}
int push(sqstack &s,char c)
{ if(s.top-s.base>=s.stacksize){
s.base=(char *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(char));
if(!s.base) exit(0);
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT; }
*s.top++=c;
return 1;
}
int judge(sqstack &s,sqstack &q)
{ int n;
do
{ if( (*s.top--)-(*q.top--)!=0) {n=0;break;}/*请问我这里有啥问题么?*/
else n=1;
} while(q.top!=q.base);
return n;
}
pop(sqstack &s,char e){
if(s.top==s.base)return 0;
e=*--s.top;
return 1;
}
main()
{ sqstack s,q,p;char c;int n;
initstack (s);
initstack (q);
initstack (p);
printf("put a charity end up with '@'\n");
while(c!='@')
{ scanf("%c",&c);
push(s,c);
push(p,c);
}
do
{pop(p,c);push(q,c);}
while(p.top!=p.base);
n=judge(s,q);
if(n==1) printf("Yes,it is \n");
if(n==0) printf("No,it is not\n");
}