入栈与出栈的所有排列可能性

  • 格式:doc
  • 大小:49.50 KB
  • 文档页数:39

#include #include #include #include #include /*定义全局变量*/ int pu=0,po=0,t=0; char tp[130][12];

/*用栈排出入栈出栈的顺序*/ struct train{ int numb; struct train *next; };

struct push{ int a; int b; char data[24]; struct push *next; }; struct push *creat(struct push *top) { top=(struct push *)malloc(sizeof(struct push)); top->next=NULL; return top; }

struct push *pup(struct push *top,int a,int b,int n) { struct push *p,*q; q=top->next; p=(struct push *)malloc(sizeof(struct push)); p->a=a; p->b=b; if(q) strcpy(p->data,q->data); if(q->a { p->data[a+b-1]='r'; p->data[a+b]='\0'; } else { p->data[a+b-1]='c'; p->data[a+b]='\0'; } p->next=top->next; top->next=p; return top; }

struct push *pop(struct push *top) { top=top->next; return top; }

struct push *apaili(struct push *top,int numb) /*向后移动一个出命令*/ { struct push *q; q=top->next; if(pu { pu++; top=pup(top,pu,po,numb); top=apaili(top,numb); } if(po { po++; top=pup(top,pu,po,numb); top=apaili(top,numb); } return top; }

struct push *bpaili(struct push *top,int numb) /*移动第一个出命令到最后*/ { int a,b,cir; char x[22]; struct push *p; do{ p=top->next; p=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=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) return top; top=pop(top); top=bpaili(top,numb);

} int jc(char c[22]) { int i=0,k=0; if(c[i]!=0) do{ if(c[i]=='r') k++; if(c[i]=='c') k--; if(k<0) return 0; i++; }while(c[i]!='\0'); return 1; }

/*调用排好的顺序进行入栈与出栈操作*/ struct train *tcreat(struct train *ttop) { ttop=(struct train *)malloc(sizeof(struct train)); ttop->next=NULL; return ttop; }

struct train *tpup(struct train *ttop,int data) { struct train *p; p=(struct train *)malloc(sizeof(struct train)); p->numb=data; p->next=ttop->next; ttop->next=p; return ttop; }

struct train *tpop(struct train *ttop) { int data; struct train *p; p=ttop->next; data=p->numb; ttop=ttop->next; printf("%d ",data); return ttop; }

inso(char c[22],int n) { int i,k=0,begin[12]; struct train *ttop; ttop=tcreat(ttop); for(i=0;i begin[i]=i+1; i=0; if(c[i]!='\0') while(c[i]!='\0') { switch(c[i]) { case 'r': ttop=tpup(ttop,begin[k]); k++; break; case 'c': ttop=tpop(ttop); break; } i++; } }

/*输出所有情况*/ print(int k) { int i,j=0,m; for(i=0;i { if(i%2==0) { m=8+j; gotoxy(5,m); } j++; printf("第%3d种排列:",i+1); inso(tp[i],k); gotoxy(32,m); if((i+1)%16==0) { getch(); j=0; } } }

/*产生随机数功能函数*/ int ram() { int a,b; b=random(100); if(b<=100) a=4; if(b<=60) a=3; if(b<=30) a=2; if(b<=10) a=1; return a; }

int aram() { int a,b; b=random(120); if(b<=120) a=3; if(b<=60) a=2; if(b<=20) a=1; if(b<=5) a=0; return a; } /*清屏功能函数*/ void clears(int x,int y,int n,int m) { setfillstyle(1,8); bar(x,y,n,m);

} /*提示栏信息库*/ tel(int n) { switch(n) { case 1:clears(459,300,625,465); gotoxy(59,21); printf("这是输入平台一"); gotoxy(59,23); printf("输入要调用货车厢个数"); gotoxy(59,25); printf("它必须不大于现有个数"); break; case 2:clears(459,300,625,465); gotoxy(59,21);