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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現雙向鏈表(兩個版本)

Java實現雙向鏈表(兩個版本)

2020-03-28 11:15MRR JAVA教程

這篇文章主要介紹了Java實現雙向鏈表(兩個版本)的相關資料,需要的朋友可以參考下

臨近春節,項目都結束了,都等著回家過年了。下面是小編給大家研究數據結構的相關知識,鏈表算是經常用到的一種數據結構了,現將自己的實現展示如下,歡迎大神賜教。

第一個版本,沒有最后一個節點,每次從根節點開始遍歷

?
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
public class LinkedList<E> {
private Node head;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=head;
if(lst==null){
this.head=new Node(e, null, null);
return this;
}else{
while(true){
if(lst.next==null){
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除這個元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**節點信息*/
private class Node{
public Node pre;
public E value;
public Node next;
 
public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
}
}

第二個版本,有了最后一個節點

?
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
public class LinkedList<E> {
private Node head;
private Node last;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public E getLast(){
if(last==null){
return null;
}
return last.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=last;
if(lst==null){//如果最后一個節點是空的則這個鏈表就是空的
this.last=new Node(e, null, null);
this.head=this.last;
return this;
}else{
while(true){
if(lst.next==null){//
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
last=lst.next;
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除這個元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**節點信息*/
private class Node{
public Node pre;
public E value;
public Node next;
 
public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
}
}

注:以上兩個版本都沒有考慮在多線程下使用的情況。

以上所述是小編給大家介紹的Java實現雙向鏈表(兩個版本)的相關知識,希望對大家有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 白丝尤物的下面被疯狂蹂躏 | 免费在线影院 | 1769在线观看| 教师系列 大桥未久在线 | 成在线人免费视频一区二区三区 | 叛佛 作者满栀小说免费阅读 | a毛片免费全部在线播放毛 a级在线看 | 亚洲精品国偷拍自产在线观看蜜臀 | 99国产牛牛视频在线网站 | 91精品啪在线观看国产老湿机 | 国产做a爰片久久毛片 | 午夜香蕉成视频人网站高清版 | 91九色porny国产美女一区 | 91麻豆制片厂 | 国产午夜精品理论片 | 校花小雪灌满了男人们的浓浆 | 60老妇性xxxxhd | 日韩一区在线播放 | 国产精品香蕉夜间视频免费播放 | 亚洲高清无在码在线电影 | 任我行视频在线观看国语 | 东京干福利 | 欧美亚洲国产一区二区三区 | 99久久精品免费看国产一区二区 | 亚洲欧美国产精品久久久 | 日本欧美一二三区色视频 | 精品国产91久久久久久久 | 日韩欧美视频二区 | 成年美女黄网站色视频大全免费 | 99九九精品免费视频观看 | 91麻豆精品国产片在线观看 | 欧美一卡2卡3卡无卡 | 女女同性做爰xxoo亲吻 | 亚洲精品一区波多野结衣 | 亚洲香蕉综合在人在线视看 | sao虎在线精品永久在线 | 春色视频网站 | 小鸟酱视频在线观看 | 亚洲欧美成人综合久久久 | 欧美色fx性乌克兰 | 精品国语对白精品自拍视 |