一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 實現雙向鏈表實例詳解

java 實現雙向鏈表實例詳解

2020-09-02 09:55Java教程網 Java教程

這篇文章主要介紹了java 實現雙向鏈表實例詳解的相關資料,需要的朋友可以參考下

java 實現雙向鏈表實例詳解

 雙向鏈表是一個基本的數據結構,在Java中LinkedList已經實現了這種結構,不過作為開發者,也要擁有自己顯示這種結構的能力。話不多說,上代碼:

    首先是鏈表的節點類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * 鏈表節點
 * @author Administrator
 *
 * @param <T>
 */
public class ChainNode<T> {
 private T data;
 //對象編號
 private int dataNo;
  
 public ChainNode<T> nextChainNode;
 public ChainNode<T> preChainNode;
 public ChainNode(T data, ChainNode<T> nextChainNode,
   ChainNode<T> preChainNode) {
  this.data = data;
  this.nextChainNode = nextChainNode;
  this.preChainNode = preChainNode;
 }
  
  
  
 public ChainNode(T data) {
  this.data = data;
 }
 
  
 
 public int getDataNo() {
  return dataNo;
 }
 
 
 
 public void setDataNo(int dataNo) {
  this.dataNo = dataNo;
 }
 
 
 public void setData(T data) {
  this.data = data;
 }
 
 
 
 public T getData() {
  return data;
 }
  
  
}

 然后是鏈表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<pre name="code" class="java">/**
 * 鏈表實現類
 * @author Administrator
 *
 * @param <T>
 */
public class Chain<T> {
 //頭節點
 private ChainNode<T> headNode;
 //末尾節點指針
 private ChainNode<T> lastNode;
 private int size;
  
  
 //創建鏈表就創建頭節點
 public Chain() {
  this.headNode = new ChainNode<T>(null);
  this.lastNode = headNode;
   
 }
 //增加一個節點
 public void addNode(T data) {
  ChainNode<T> node = new ChainNode<T>(data);
  if(lastNode != null){
   lastNode.nextChainNode = node;
   node.preChainNode = node;
   node.setDataNo(size);
   lastNode = node;
   size++;
  }
 }
 //刪除指定索引的節點
 public void deleteNode(int dataNo) throws Exception {
  if(getSize() == 0){
   throw new Exception("chain is empty");
  }
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
   if(node.getDataNo() == dataNo){
    node.preChainNode.nextChainNode = node.nextChainNode;
    if(node.nextChainNode != null){
     node.nextChainNode.preChainNode = node.preChainNode;
    }
     
    node.nextChainNode = null;
    node.preChainNode = null;
    size--;
    //重新設置節點的編號
    for (ChainNode<T> chainNode = node.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
     chainNode.setDataNo(chainNode.getDataNo()-1);
    }
    return;
     
   }
  }
  throw new Exception("the corresponding data that can not be found");
 }
 //獲取指定索引的節點
 public T get(int dataNo) throws Exception {
  if(getSize() == 0){
   throw new Exception("chain is empty");
  }
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
   if(node.getDataNo() == dataNo){
    return node.getData();
   }
  }
  throw new Exception("the corresponding data that can not be found");
 }
 //修改對應索引的節點
 public void set(int dataNo,T data) throws Exception {
  if(getSize() == 0){
   throw new Exception("chain is empty");
  }
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
   if(node.getDataNo() == dataNo){
    node.setData(data);
    return;
   }
  }
  throw new Exception("the data that is to be modified can not be found");
 }
 //是否包含對應數據的節點
 public boolean isContains(T data) throws Exception {
  if(getSize() == 0){
   throw new Exception("chain is empty");
  }
  for (ChainNode<T> chainNode = headNode.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
   if(chainNode.getData() == data){
    return true;
   }
  }
   
  return false;
 }
 //獲取節點數量(不含頭節點)
 public int getSize() {
  return size;
 }
}

測試一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class ChainTest {
 public static void main(String[] args) throws Exception{
  String oneString = "one";
  String twoString = "two";
  String threeString = "three";
  String fourString = "four";
  Chain<String> chain = new Chain<String>();
  chain.addNode(oneString);
  chain.addNode(twoString);
  chain.addNode(threeString);
  chain.addNode(fourString);
  for (int i = 0; i < chain.getSize(); i++) {
   String string2 = chain.get(i);
   System.out.println(string2);
  }
  try {
   String string = chain.get(2);
   System.out.println("the data of the second node is"+string);
   System.out.println(chain.isContains(fourString));
   chain.set(1, "haha");
   System.out.println("modify chain");
   for (int i = 0; i < chain.getSize(); i++) {
    String string2 = chain.get(i);
    System.out.println(string2);
   }
    
   chain.deleteNode(3);
   System.out.println("delete one node");
   for (int i = 0; i < chain.getSize(); i++) {
    String string2 = chain.get(i);
    System.out.println(string2);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
one
two
three
four
the data of the second node isthree
true
modify chain
one
haha
three
four
delete one node
one
haha
three

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/sinat_23092639/article/details/51160621

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费观看在线观看 | 十六一下岁女子毛片免费 | free哆拍拍免费永久视频 | 狠狠色狠狠色综合曰曰 | 亚洲国产精品久久精品怡红院 | 美女脱了内裤让男桶爽 | 91高清在线视频 | 日韩精品欧美国产精品亚 | 欧洲vodafone精品性 | 好大好硬好紧太深了受不了 | 国内精品露脸在线视频播放 | 黑人巨荃大战乌克兰美女 | 欧美一区二区三区在线观看免费 | 国产99re在线观看69热 | 国产精品毛片久久久久久久 | 青草青草视频2免费观看 | 欧亚精品一区二区三区 | 18国产精品白浆在线观看免费 | 国产一级片视频 | 国产二区视频 | 91免费播放人人爽人人快乐 | 欧美肥b | 91美女在线视频 | 小sao货水好多真紧h的视频 | 午夜一级| 好大好硬好深好爽想要之黄蓉 | 国产高清国内精品福利色噜噜 | 免费看3d小舞被躁视频网站 | 日本人和黑人一级纶理片 | 色婷婷婷丁香亚洲综合不卡 | 日本在线看免费 | 色香婷婷 | 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | 我被黑人彻底征服的全文 | 四虎影视免费观看 | 亚洲品质自拍视频 | 亚欧成人中文字幕一区 | aⅴ天堂小视频 | 黑人巨荃大战乌克兰美女 | 成人免费观看一区二区 | 国产一区二区精品久久91 |