JAVA:冒泡排序和链表.docx_第1页
JAVA:冒泡排序和链表.docx_第2页
JAVA:冒泡排序和链表.docx_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

冒泡排序和链表一 冒泡排序1. /*功能:用Java语言实现冒泡排序2. public class BubbleSort2 3. public void sort(int data) 4. int temp;5. for (int i = 0; i i; j-) 7. if (datai dataj成立,则交换两个数8. temp = datai;9. datai = dataj;10. dataj = temp;11. 12. 13. 14. 15. public static void main(String args) 16. BubbleSort2 bubbleSort2 = new BubbleSort2();17. int a = 4, 2, 3 ,7,9;18. for (int i = 0; i a.length; i+)19. System.out.print(ai + );20. System.out.println(n降序排序);21. bubbleSort2.sort(a);22.23. System.out.println(n after sort:);24. for (int i = 0; i a.length; i+)25. System.out.print(ai + );26. 27. 二. 链表结构构造一个链表模型1. public class Node2. 3. private int data=-1;4. private Node prev=null;5. private Node next=null; /链表的节点6. public int getData() 7. 8. return data;9. 10. public void setData(int data)11. 12. this.data = data; /放数据13. 14. public Node getNext() 15. 16. return next; /获取下一个节点的数据17. 18. public void setNext(Node next) 19. 20. this.next = next; /设置下一个节点的数据21. 22. public Node getPrev()23. 24. return prev; /返回上一个节点的数据25. 26. public void setPrev(Node prev)27. 28. this.prev = prev; /设置上一个节点的数据29. 30. 测试链表31. public class Test2 32. private Node doubleDirList=null;33. private void createDoubleDirList() throws IOException34. 35. /输入36. String numbers=;37. System.out.println(0-9的随机系列,用,格开:);38. BufferedReader br=new BufferedReader(new InputStreamReader(System.in);39. numbers=br.readLine();40. br.close();41. /解析输入42. StringTokenizer tokenizer=new StringTokenizer(numbers,);43. doubleDirList=new Node();44. doubleDirList.setData(new Integer(tokenizer.nextToken().intValue();45. Node current=doubleDirList;46. while(tokenizer.hasMoreTokens()47. 48. Node newNode=new Node();49. newNode.setData(new Integer(tokenizer.nextToken().intValue();50. newNode.setPrev(current);51. current.setNext(newNode);52. current=newNode;53. 54. 55. / 正向输出56. private void printFromFirstToLast()57. 58. String result=;59. System.out.println(正向输出:);60. Node current=doubleDirList;61. while(current!=null)62. 63. result+=current.getData()+ ;64. current=current.getNext();65. 66. System.out.println(result);67. 68. public static void main(String args)69. 70. Test2 test2=new Test2();71. try 72. 73. test2.createDoubleDirLis

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论