java数据结构之双向链表

女爷i 2021-10-14 04:42 508阅读 0赞
  1. public class DoubleLinkedList<E> {
  2. private Node first;//指向第一个元素
  3. private Node last;//指向最后一个元素
  4. private int length=0;//链表长度
  5. class Node {
  6. private Node previous;
  7. private Node next;
  8. private E e;
  9. public Node(Node previous, Node next, E e) {
  10. this.previous = previous;
  11. this.next = next;
  12. this.e = e;
  13. }
  14. }
  15. /***
  16. * 向头节点添加元素,节点结构对外应该是不可见的,所以这里只传递一个泛型的值e
  17. */
  18. public void addFirst(E e) {
  19. if (first == null) {//链表为空判断
  20. Node node = new Node(null, null, e);//创建一个新的节点,前驱和后继都为空
  21. this.first = node;
  22. this.last=node;//将first和last指针指向链表的第一个元素
  23. length++;//链表长度自增一,下同
  24. }else{
  25. Node node=new Node(null,first,e);//链表不为空创建一个前驱为空,后继为当前first节点的节点,值为传入的参数e
  26. this.first.previous=node;//当前first的前驱设置为node
  27. this.first=node;//将first指针指向新节点
  28. length++;
  29. }
  30. }
  31. /***
  32. *addLast同addFirst
  33. */
  34. public void addLast(E e) {
  35. if (last == null) {
  36. Node node = new Node(null, null, e);
  37. this.first = node;
  38. this.last=node;
  39. length++;
  40. }else{
  41. Node node=new Node(last,null,e);
  42. this.last.next=node;
  43. this.last=node;
  44. length++;
  45. }
  46. }
  47. public void insertPrevious(E baseElement,E value){
  48. Node index=this.first;
  49. while(index!=null){
  50. if(index.e==baseElement)break;
  51. index=index.next;
  52. }
  53. Node insertValue=new Node(index.previous,index,value);
  54. index.previous.next=insertValue;
  55. index.previous=insertValue;
  56. length++;
  57. }
  58. public void insertNext(E baseElement,E value){
  59. Node index=this.first;
  60. while(index!=null){
  61. if(index.e==baseElement)break;
  62. index=index.next;
  63. }
  64. Node insertValue=new Node(index,index.next,value);
  65. index.next.previous=insertValue;
  66. index.next=insertValue;
  67. length++;
  68. }
  69. public void removeElement(E value){
  70. Node index=this.first;
  71. while(index!=null){
  72. if(index.e==value)break;
  73. index=index.next;
  74. }
  75. index.previous.next=index.next;
  76. index.next.previous=index.previous;
  77. length--;
  78. }
  79. public int getLength(){
  80. return length;
  81. }
  82. @Override
  83. public String toString() {
  84. StringBuffer sb=new StringBuffer();
  85. Node current=this.first;
  86. while(current!=null){
  87. sb.append(current.e+"->");
  88. current=current.next;
  89. }
  90. return sb.toString();
  91. }
  92. public static void main(String[] args) {
  93. DoubleLinkedList<String> list=new DoubleLinkedList<>();
  94. list.addLast("value1");
  95. list.addLast("value2");
  96. list.addLast("value3");
  97. list.addLast("value4");
  98. list.addFirst("value0");
  99. list.insertPrevious("value3","insertValue");
  100. list.insertNext("value3","insertValue2");
  101. System.out.println(list.toString());
  102. System.out.println("链表的长度是"+list.getLength());
  103. list.removeElement("value3");
  104. System.out.println(list.toString());
  105. System.out.println("链表的长度是"+list.getLength());
  106. }
  107. }

发表评论

表情:
评论列表 (有 0 条评论,508人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数据结构--双向

    数据结构-链表-双向链表 单向链表和环形链表都是属于拥有方向性的链表,只能单向遍历,如果由于某种原因造成了某个连接断裂,那后面的链表数据便会遗失而无法复原。所以,我们可以

    相关 数据结构双向

    双向链表,每个节点除了保存了对下一个节点的引用,同时还保存这对前一个节点的引用。 其结点跟单链表相似,如图所示: ![这里写图片描述][SouthEast] 设计双向