双向链表的简单Java实现-sunziren
Posted sunziren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双向链表的简单Java实现-sunziren相关的知识,希望对你有一定的参考价值。
写在前面,csdn的那篇同名博客就是我写的,我把它现在在这边重新发布,因为我实在不想用csdn了,那边的广告太多了,还有就是那个恶心人的“阅读更多”按钮,惹不起我躲得起。
在上次分享完单向链表的简单编写后,索性对于双向链表进行了一定的了解,在上次的基础上进行了一定程度的改进,做了一个非循环的双向链表。
双向链表的特点在于每个节点不仅知道自己的下属节点而且还知道自己的父节点。在双向链表的逆序方法中,我使用的队列来支持这个操作,利用了队列的先进先出的特点。
1 package demo_4; 2 3 import java.util.Stack; 4 5 public class MyDoubleList<Re_Helix> { 6 //节点内部类; 7 private class Node{ 8 private Re_Helix data; //数据; 9 private Node next = null; //下个节点的引用; 10 private Node previous = null; //上个节点的引用; 11 12 public Node() { //节点的无参构造; 13 super(); 14 } 15 16 public Node(Re_Helix data) { //节点的有参构造; 17 super(); 18 this.data = data; 19 } 20 } 21 22 private Node head; //头部节点; 23 private Node end; //尾部节点; 24 private Node point; //临时节点; 25 private Node point_pre; //临时节点2; 26 private int length; //长度属性; 27 28 public MyDoubleList() { //链表的无参构造; 29 head = new Node(); 30 end = head; 31 length = 0; 32 } 33 34 public int length() { //返回链表的长度; 35 return length; 36 } 37 38 public void showAll() { //在控制台查看当前链表中的所有数据 39 point = head; 40 int i = 0; 41 while(point!=null) { 42 System.out.println("第"+(i++)+"个:"+point.data); 43 point = point.next; 44 } 45 } 46 47 public Re_Helix getById(int target) { //输入下标返回值; 48 return packPoint(target).data; 49 } 50 51 public void input(Re_Helix data) { 52 point = new Node(data); 53 if(length==0) { 54 end.data = point.data; 55 }else { 56 point_pre = end; 57 end.next = point; 58 end = point; 59 point.previous = point_pre; 60 } 61 length++; 62 } 63 64 public void inputById(int target,Re_Helix data) { 65 point = packPoint(target); 66 Node temp = new Node(data); 67 if(target>=length) { 68 end.next = temp; 69 temp.previous = end; 70 end = temp; 71 }else if(target<=0) { 72 temp.next = head; 73 head.previous = temp; 74 head = temp; 75 }else { 76 temp.next = point; 77 temp.previous = point.previous; 78 point.previous.next = temp; 79 point.previous = temp; 80 } 81 length++; 82 } 83 84 public void deleteById(int target) { //输入下标删除值 85 if(target>0) { 86 packPoint(target-1).next = packPoint(target).next; 87 packPoint(target).next.previous = packPoint(target-1); 88 }else { 89 head.next.previous = null; 90 head = head.next; 91 } 92 length--; 93 } 94 95 public void deleteAll() { //清空链表; 96 length = 0; 97 head.data = null; 98 head.next = null; 99 point = null; 100 end = head; 101 System.gc(); 102 } 103 104 public boolean editById(int target,Re_Helix data) { //修改传入下标位置的值; 105 if(target<0 || target>length) { 106 return false; 107 }else { 108 packPoint(target).data = data; 109 return true; 110 } 111 } 112 113 public void reverse() { //将链表反转; 114 Stack<Node> s1 = new Stack<Node>(); //利用队列的先进先出的特性; 115 point = head; 116 while(point!=null) { 117 s1.push(point); 118 point = point.next; 119 } 120 head = s1.pop(); 121 head.previous = null; 122 point = head; 123 while(!s1.isEmpty()) { 124 point.next = s1.pop(); 125 point_pre = point; 126 point = point.next; 127 point.previous = point_pre; 128 } 129 end = point; 130 end.next = null; //要将逆序后的end位置节点的next置空,不然会造成最后两位的循环; 131 } 132 133 private Node packPoint(int target) { //内部方法,将指针指向指定下标的节点; 134 if(target<=0) { 135 point = head; 136 }else if(target>=length) { 137 point = end; 138 }else { 139 int i = 0; 140 point = head; 141 while(i++!=target) { 142 point = point.next; 143 } 144 } 145 return point; 146 }
}
多有不足,欢迎评论区批评指正。
以上是关于双向链表的简单Java实现-sunziren的主要内容,如果未能解决你的问题,请参考以下文章