在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java实现单链表、双向链表

2016-7-29 15:47| 发布者: zhangjf| 查看: 584| 评论: 0

摘要: 本文实例为大家分享了java实现单链表、双向链表的相关代码,供大家参考,具体内容如下 java实现单链表: package code; class Node { Node next; int data; public Node(int data) { this.data=data; } ...

本文实例为大家分享了java实现单链表、双向链表的相关代码,供大家参考,具体内容如下

java实现单链表:

  1. package code;
  2. class Node
  3. {
  4. Node next;
  5. int data;
  6. public Node(int data)
  7. {
  8. this.data=data;
  9. }
  10. }
  11. class LinkList
  12. {
  13. Node first;
  14. //头部
  15. public LinkList()
  16. {
  17. this.first=null;
  18. }
  19. public void addNode(Node no)
  20. {
  21. no.next=first;
  22. first=no;//在头部添加
  23. }
  24. public void delectNode()
  25. {
  26. Node n=first.next;
  27. first=null;
  28. first=n;//在头部删除
  29. }
  30. //删除指定位置
  31. public int Number()
  32. {
  33. int count=1;
  34. //查看有多少元素
  35. Node nd=first;
  36. while(nd.next!=null)
  37. {
  38. nd=nd.next;
  39. count++;
  40. }
  41. return count;
  42. }
  43. public void delectExact(int n)
  44. {
  45. //删除指定位置
  46. if(n>1)
  47. {
  48. int count=1;
  49. Node de=first;
  50. while(count<n-1)
  51. {
  52. de=de.next;
  53. count++;
  54. }
  55. de.next=de.next.next;
  56. }
  57. else
  58. first=first.next;
  59. }
  60. public void addExact(int n,Node nd)
  61. {
  62. if(n>1)//添加指定位置
  63. {
  64. int count=1;
  65. Node de=first;
  66. while(count<n-1)
  67. {
  68. de=de.next;
  69. count++;
  70. }
  71. nd.next=de.next;
  72. de.next=nd;
  73. }
  74. else
  75. first=first.next;
  76. }
  77. public int findNode(int n)
  78. {
  79. int count=1;//查找一个数对应的位置
  80. Node de=first;
  81. while(de.data!=n)
  82. {
  83. de=de.next;
  84. count++;
  85. if(de==null)
  86. {
  87. return -1;
  88. }
  89. }
  90. return count;
  91. }
  92. public void print()
  93. {
  94. Node no=first;//打印所有
  95. while(no!=null)
  96. {
  97. System.out.println(no.data);
  98. no=no.next;
  99. }
  100. }
  101. }
  102. public class TextNode
  103. {
  104. public static void main(String[] args)
  105. {
  106. LinkList ll=new LinkList();
  107. ll.addNode(new Node(12));
  108. ll.addNode(new Node(15));
  109. ll.addNode(new Node(18));
  110. ll.addNode(new Node(19));
  111. ll.addNode(new Node(20));
  112. /*System.out.println(ll.first.data);
  113. ll.delectNode();
  114. System.out.println(ll.first.data);*/
  115. System.out.println(ll.Number());
  116. ll.delectExact(3);
  117. ll.addExact(3, new Node(100));
  118. System.out.println(ll.Number());
  119. // ll.print();
  120. System.out.println(ll.findNode(112));
  121. }
  122. }
复制代码

java实现双向链表:

  1. public class DoubleLink
  2. {
  3. public static void main(String[]args)
  4. {
  5. Node2 no=new Node2(5);
  6. no.addLeft(new Node2(6));
  7. no.addRight(new Node2(7));
  8. /*no.print();
  9. no.print2();*/
  10. no.addExact2(1, new Node2(8));
  11. no.print();
  12. System.out.println("--------------");
  13. no.print2();
  14. }
  15. }
  16. class Node2
  17. {
  18. public Node2 first;
  19. public Node2 end;
  20. public Node2 left;
  21. public Node2 right;
  22. int data=0;
  23. public Node2(int n)
  24. {
  25. first=this;
  26. end=this;
  27. first.data=n;
  28. }
  29. //从头部添加
  30. public void addLeft(Node2 before)
  31. {
  32. first.left=before;
  33. before.right=first;
  34. first=before;
  35. }
  36. //从尾部添加
  37. public void addRight(Node2 after)
  38. {
  39. end.right=after;
  40. after.left=end;
  41. end=after;
  42. }
  43. //插入正数(第三声)的第几个
  44. public void addExact(int n,Node2 no)
  45. {
  46. int count=0;
  47. if(n==0)
  48. {
  49. addLeft(no);
  50. }
  51. else
  52. {
  53. Node2 f=first;
  54. while(true)
  55. {
  56. f=f.right;
  57. count++;
  58. if(count==n)
  59. {
  60. //此处为四个指针的指向的变化
  61. no.left=f.left;
  62. f.left.right=no;
  63. // first.left=no;
  64. no.right=f;
  65. f.left=no;
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. //插入倒数的第几个
  72. public void addExact2(int n,Node2 no)
  73. {
  74. int count=0;
  75. if(n==0)
  76. {
  77. addRight(no);
  78. }
  79. else
  80. {
  81. Node2 f=end;
  82. while(true)
  83. {
  84. f=f.left;
  85. count++;
  86. if(count==n)
  87. {
  88. no.left=f;
  89. no.right=f.right;
  90. f.right.left=no;
  91. f.right=no;
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. //正序遍历
  98. public void print()
  99. {
  100. System.out.println(first.data);
  101. while(first.right!=null)
  102. {
  103. System.out.println(first.right.data);
  104. first=first.right;
  105. }
  106. // System.out.println(end.data);
  107. }
  108. //倒序遍历
  109. public void print2()
  110. {
  111. System.out.println(end.data);
  112. while(end.left!=null)
  113. {
  114. System.out.println(end.left.data);
  115. end=end.left;
  116. }
  117. }
  118. }
  119. /*值得注意的是,每一次插入一个新的对象的时候,需要注意指针指向的改变。
  120. 首先是这个新的对象两边的指向(左和右),其次是时左边的对象向右的指向
  121. 和右边对象向左的指向。
  122. 这四个指针的指向必须正确,否则可能导致正序或者倒序遍历无法实现。
  123. */
  124. /*对比单链表,单链表只能从一个方向遍历,因为只有一个头,而双向链表,有头和尾,可以从
  125. * 头遍历,也可以从尾遍历,而且其中一个对象因为有两个方向的指针,所以他可以获得左边的
  126. * 对象也可以获得右边的对象。
  127. * 但是单链表的话,因为只有一个方向,所以只能向左或右。添加对象的时候,双向也可以从头添加,也可以从尾添加。
  128. * 如果单链表要实现两个方向添加比较难得,或者说不行,因为他只有向左或向右的一个方向的指针
  129. * 而双向链表每个对象都有两个方向的指针没这样更灵活,但是这同样有缺点,因为这样的话每个对象
  130. * 都会包含两个指针,这同样内存会消耗更多。
  131. *
  132. * */
复制代码

以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 15:59

Copyright 2015-2025 djqfx

返回顶部