欢迎来到天天文库
浏览记录
ID:35733051
大小:482.00 KB
页数:7页
时间:2019-04-15
《使用栈求n个元素的所有排列》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.使用栈求n个元素的所有排列1.实验目的熟悉栈的ADT以及它的方法实现,学会用栈的方法去解决一些问题。2.实验要求求n个元素的所有排列,要求:使用栈。例如n=3时得:123、132、213、231、312、3213.算法设计publicvoidpailie(intn){ArrayStackstack=newArrayStack();for(inta=1;a<=n;a++)stack.push(a);System.out.println(stack.toString());booleanback=false;whil
2、e(!stack.isEmpty()&&!back){inti=stack.pop();for(;i3、ackException;publicinterfaceStackADT{publicintsize();publicbooleanisEmpty();publicvoidpush(intn);publicintpop()throwsEmptyStackException;publicintpeek();publicStringtoString();}2.ArrayStack:importjava.util.EmptyStackException;publicclassArrayStackimplementsSta4、ckADT{privateObject[]stack;publicinttop;privatefinalintDEFAULT_CAPACITY=20;publicArrayStack(){stack=newObject[DEFAULT_CAPACITY];top=-1;}publicArrayStack(intsize){stack=newObject[size];top=-1;}publicintsize(){returntop+1;}publicbooleanisEmpty(){returntop==-1;}p5、ublicvoidpush(intn){if(top+1==stack.length)expandCapacity();stack[++top]=n;}privatevoidexpandCapacity(){Object[]newstack=newObject[stack.length*2+1];for(inti=0;i6、)thrownewEmptyStackException();return(Integer)stack[top--];}publicintpeek(){return(Integer)stack[top];}publicStringtoString(){Stringstring="";for(inti=0;i<=top;i++)string=string+stack[i].toString();returnstring;}publicbooleanfind(intk){booleanresult=false;for(7、inti=0;i<=top;i++){if(k==(Integer)stack[i]){result=true;break;}}returnresult;}}3.Expression:publicclassExpression{publicvoidpailie(intn){ArrayStackstack=newArrayStack();for(inta=1;a<=n;a++)stack.push(a);System.out.println(stack.toString());booleanback=false;wh8、ile(!stack.isEmpty()&&!back){inti=stack.pop();for(;i
3、ackException;publicinterfaceStackADT{publicintsize();publicbooleanisEmpty();publicvoidpush(intn);publicintpop()throwsEmptyStackException;publicintpeek();publicStringtoString();}2.ArrayStack:importjava.util.EmptyStackException;publicclassArrayStackimplementsSta
4、ckADT{privateObject[]stack;publicinttop;privatefinalintDEFAULT_CAPACITY=20;publicArrayStack(){stack=newObject[DEFAULT_CAPACITY];top=-1;}publicArrayStack(intsize){stack=newObject[size];top=-1;}publicintsize(){returntop+1;}publicbooleanisEmpty(){returntop==-1;}p
5、ublicvoidpush(intn){if(top+1==stack.length)expandCapacity();stack[++top]=n;}privatevoidexpandCapacity(){Object[]newstack=newObject[stack.length*2+1];for(inti=0;i6、)thrownewEmptyStackException();return(Integer)stack[top--];}publicintpeek(){return(Integer)stack[top];}publicStringtoString(){Stringstring="";for(inti=0;i<=top;i++)string=string+stack[i].toString();returnstring;}publicbooleanfind(intk){booleanresult=false;for(7、inti=0;i<=top;i++){if(k==(Integer)stack[i]){result=true;break;}}returnresult;}}3.Expression:publicclassExpression{publicvoidpailie(intn){ArrayStackstack=newArrayStack();for(inta=1;a<=n;a++)stack.push(a);System.out.println(stack.toString());booleanback=false;wh8、ile(!stack.isEmpty()&&!back){inti=stack.pop();for(;i
6、)thrownewEmptyStackException();return(Integer)stack[top--];}publicintpeek(){return(Integer)stack[top];}publicStringtoString(){Stringstring="";for(inti=0;i<=top;i++)string=string+stack[i].toString();returnstring;}publicbooleanfind(intk){booleanresult=false;for(
7、inti=0;i<=top;i++){if(k==(Integer)stack[i]){result=true;break;}}returnresult;}}3.Expression:publicclassExpression{publicvoidpailie(intn){ArrayStackstack=newArrayStack();for(inta=1;a<=n;a++)stack.push(a);System.out.println(stack.toString());booleanback=false;wh
8、ile(!stack.isEmpty()&&!back){inti=stack.pop();for(;i
此文档下载收益归作者所有