欢迎来到天天文库
浏览记录
ID:51793995
大小:32.13 KB
页数:4页
时间:2020-03-15
《基于java链表的冒泡排序.docx》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、/**Tochangethistemplate,chooseTools
2、Templates*andopenthetemplateintheeditor.*//****@authorC.J.Ward*/classNode{intdate;Nodenext;publicNode(intdata,Nodenext){this.date=data;this.next=next;}publicNode(intdata){this.date=data;this.next=null;}@OverridepublicStringtoString(){return"{"+date
3、+"}";}}publicclasssort{Nodehead;publicvoidsinglglist(){this.head=null;}publicbooleanadd(intelement){Nodenode=newNode(element);if(this.head==null){this.head=node;returntrue;}if(this.head!=null){Nodep=this.head;while(p.next!=null){p=p.next;//System.out.print(p.date+",");}p.next=node;}/
4、/returntrue;}publicbooleanadd(intindex,intelement)//����element�������������Ϊindex{//�������ɹ�����true��O(n)if(this.head==null
5、
6、index<=0)//ͷ����this.head=newNode(element,this.head);else//�����?����index>=1{intj=0;Nodep=this.head;while(p.next!=null&&j7、}p.next=newNode(element,p.next);//�м�/β����}returntrue;}@OverridepublicStringtoString(){Stringstr="";Nodep=this.head;while(p!=null){//str+=p.date;System.out.print(p.date+",");p=p.next;}returnstr;}publicbooleansort(){if(this.head==null){returnfalse;}if(this.head.next==null){returnfals8、e;}Nodef=null;if(this.head.date>this.head.next.date){f=this.head;this.head=this.head.next;add(1,f.date);}Nodeq=null,front=this.head,t=null;inti=1;while(front.next.next!=null){Nodep=front.next;if(p.date>p.next.date){front.next=front.next.next;add(i+1,p.date);//System.out.print(p.date+9、",");//p=p.next;}i++;front=front.next;}returntrue;//}//returntrue;}publicstaticvoidmain(Stringargs[]){sortsofe=newsort();intsort[]={4,5,8,1,2,7,3,6};System.out.print("关键字序列:");for(inti=0;i<=sort.length-1;i++){sofe.add(sort[i]);System.out.print(sort[i]+",");}System.out.println();//sof10、e.toString();for(inti=0;i
7、}p.next=newNode(element,p.next);//�м�/β����}returntrue;}@OverridepublicStringtoString(){Stringstr="";Nodep=this.head;while(p!=null){//str+=p.date;System.out.print(p.date+",");p=p.next;}returnstr;}publicbooleansort(){if(this.head==null){returnfalse;}if(this.head.next==null){returnfals
8、e;}Nodef=null;if(this.head.date>this.head.next.date){f=this.head;this.head=this.head.next;add(1,f.date);}Nodeq=null,front=this.head,t=null;inti=1;while(front.next.next!=null){Nodep=front.next;if(p.date>p.next.date){front.next=front.next.next;add(i+1,p.date);//System.out.print(p.date+
9、",");//p=p.next;}i++;front=front.next;}returntrue;//}//returntrue;}publicstaticvoidmain(Stringargs[]){sortsofe=newsort();intsort[]={4,5,8,1,2,7,3,6};System.out.print("关键字序列:");for(inti=0;i<=sort.length-1;i++){sofe.add(sort[i]);System.out.print(sort[i]+",");}System.out.println();//sof
10、e.toString();for(inti=0;i
此文档下载收益归作者所有