欢迎来到天天文库
浏览记录
ID:8809654
大小:49.50 KB
页数:39页
时间:2018-04-08
《入栈与出栈的所有排列可能性》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include#include#include#include/*定义全局变量*/intpu=0,po=0,t=0;chartp[130][12];/*用栈排出入栈出栈的顺序*/structtrain{ intnumb; structtrain*next; };structpush{ inta; intb; chardata[24]; structpush*next; };structpush*creat(structpush*top){ t
2、op=(structpush*)malloc(sizeof(structpush)); top->next=NULL; returntop;}structpush*pup(structpush*top,inta,intb,intn){ structpush*p,*q; q=top->next; p=(structpush*)malloc(sizeof(structpush)); p->a=a; p->b=b; if(q) strcpy(p->data,q->data); if(q->a3、4、!q) { p->data[a+b-1]='r'; 5、 p->data[a+b]=' '; } else { p->data[a+b-1]='c'; p->data[a+b]=' '; } p->next=top->next; top->next=p;returntop;}structpush*pop(structpush*top){ top=top->next; returntop;}structpush*apaili(structpush*top,intnumb) /*向后移动一个出命令*/{ structpush*q; q=top->next; if(pu6、pu++; top=pup(top,pu,po,numb); top=apaili(top,numb); } if(ponext; p=7、p->next; a=p->a; b=p->b; if(p->data[a+b-1]=='r') break; else { do{ top=pop(top); p=top->next; a=p->a; b=p->b; }while(p->data[a+b-1]=='c'); if(a==1) { cir=1; break; } top=pop(top); a--; b++; top=pup(top,a,b,numb); top->next->data[a+b-1]='c'; pu=a; po8、=b; top=apaili(top,numb); strcpy(x,top->next->data); if(jc(x)) { strcpy(tp[t],x); t++; } } }while(a+b<2*numb); if(cir==1) returntop; top=pop(top); top=bpaili(top,numb);}intjc(charc[22]){ inti=0,k=0; if(c[i]!=0) do{ if(c[i]=='r') k++; if(c[i]=='c') k-9、-; if(k<0) return0; i++; }while(c[i]!=' '); return1;}/*调用排好的顺序进行入栈与出栈操作*/structtrain*tcreat(structtrain*ttop){ ttop=(structtrain*)malloc(sizeof(structtrain)); ttop->next=NULL; returnttop; }structtrain*tpup(structtrain*ttop,intdata){ structtrain*p; p=(structtrain*)m10、alloc(sizeof(structtrain)); p->nu
3、
4、!q) { p->data[a+b-1]='r';
5、 p->data[a+b]=' '; } else { p->data[a+b-1]='c'; p->data[a+b]=' '; } p->next=top->next; top->next=p;returntop;}structpush*pop(structpush*top){ top=top->next; returntop;}structpush*apaili(structpush*top,intnumb) /*向后移动一个出命令*/{ structpush*q; q=top->next; if(pu6、pu++; top=pup(top,pu,po,numb); top=apaili(top,numb); } if(ponext; p=7、p->next; a=p->a; b=p->b; if(p->data[a+b-1]=='r') break; else { do{ top=pop(top); p=top->next; a=p->a; b=p->b; }while(p->data[a+b-1]=='c'); if(a==1) { cir=1; break; } top=pop(top); a--; b++; top=pup(top,a,b,numb); top->next->data[a+b-1]='c'; pu=a; po8、=b; top=apaili(top,numb); strcpy(x,top->next->data); if(jc(x)) { strcpy(tp[t],x); t++; } } }while(a+b<2*numb); if(cir==1) returntop; top=pop(top); top=bpaili(top,numb);}intjc(charc[22]){ inti=0,k=0; if(c[i]!=0) do{ if(c[i]=='r') k++; if(c[i]=='c') k-9、-; if(k<0) return0; i++; }while(c[i]!=' '); return1;}/*调用排好的顺序进行入栈与出栈操作*/structtrain*tcreat(structtrain*ttop){ ttop=(structtrain*)malloc(sizeof(structtrain)); ttop->next=NULL; returnttop; }structtrain*tpup(structtrain*ttop,intdata){ structtrain*p; p=(structtrain*)m10、alloc(sizeof(structtrain)); p->nu
6、pu++; top=pup(top,pu,po,numb); top=apaili(top,numb); } if(ponext; p=
7、p->next; a=p->a; b=p->b; if(p->data[a+b-1]=='r') break; else { do{ top=pop(top); p=top->next; a=p->a; b=p->b; }while(p->data[a+b-1]=='c'); if(a==1) { cir=1; break; } top=pop(top); a--; b++; top=pup(top,a,b,numb); top->next->data[a+b-1]='c'; pu=a; po
8、=b; top=apaili(top,numb); strcpy(x,top->next->data); if(jc(x)) { strcpy(tp[t],x); t++; } } }while(a+b<2*numb); if(cir==1) returntop; top=pop(top); top=bpaili(top,numb);}intjc(charc[22]){ inti=0,k=0; if(c[i]!=0) do{ if(c[i]=='r') k++; if(c[i]=='c') k-
9、-; if(k<0) return0; i++; }while(c[i]!=' '); return1;}/*调用排好的顺序进行入栈与出栈操作*/structtrain*tcreat(structtrain*ttop){ ttop=(structtrain*)malloc(sizeof(structtrain)); ttop->next=NULL; returnttop; }structtrain*tpup(structtrain*ttop,intdata){ structtrain*p; p=(structtrain*)m
10、alloc(sizeof(structtrain)); p->nu
此文档下载收益归作者所有