c语言递归函数检测回文,递归法判断回文字符串,急用
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
/*--------用線性表存儲字符串,結合堆棧判斷回文(關鍵判斷個數為奇或偶,奇跳過中間數據)---------*/
#include?
#include?
#define?ok?1;
#define?error?0;
typedef?char?elemtype;
typedef?struct?lnode{
lnode?*next;
elemtype?data;
}?lnode,*linklist;
bool?createlist(linklist?&h,int?n){
//尾差插法
linklist?p,q;
h=new?lnode;
h->next=NULL;
q=h;
cout<
if(n<0)?return?error;
for(int?i=0;i
p=new?lnode;
cin>>p->data;
p->next=q->next;
q->next=p;
q=q->next;
}
return?ok;
}
bool?printlist(linklist?h){
linklist?p;
p=h->next;
while(p!=NULL){
cout<data<
p=p->next;
}
return?ok;
}
#define?initsize?100
#define?stackincrs?10
typedef?struct{
elemtype?*base;
elemtype?*top;
int?stacksize;
}?stack;
bool?initstack(stack?&s){
s.base=new?elemtype?[initsize];
s.top=s.base;
s.stacksize=initsize;
return?ok;
}
bool?push(stack?&s,elemtype?e){
if(s.top-s.base>=s.stacksize){
s.base=(elemtype*)realloc(s.base,(s.stacksize+stackincrs)*sizeof(elemtype));
s.top=s.base+s.stacksize;
s.stacksize=s.stacksize+stackincrs;
}
*s.top++=e;
return?ok;
}
bool?pop(stack?&s,elemtype?&e){
if(s.top==s.base)?return?error;
--s.top;
e=*s.top;
return?ok;
}
/************************回文判別***********/
bool?jude(stack?&s,linklist?h,int?n){
//結果:flag=1為回文,flag=0不為回文
linklist?p=h->next;
for(int?i=1;i<=n/2;i++){
push(s,p->data);
p=p->next;
}
if(n%2)?p=p->next;
bool?flag=1;
elemtype?c;
while(p!=NULL&&flag){
pop(s,c);
if(c==p->data)
p=p->next;
else?flag=0;
}
return?flag;
}
void?main(){
int?n;
stack?s;
linklist?h;
initstack(s);
cout<
cin>>n;
createlist(h,n);
if?(jude(s,h,n))
cout<
else
cout<
cout<
}
總結
以上是生活随笔為你收集整理的c语言递归函数检测回文,递归法判断回文字符串,急用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hadoop2.8配置_Hadoop2.
- 下一篇: c++语言成绩统计系统数组,急求!!!关