1. 程式人生 > >雙向連結串列的簡單Java實現-sunziren

雙向連結串列的簡單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 }
  }

  多有不足,歡迎評論區批評指正。