国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)

2019-11-26 14:36:13
字體:
供稿:網(wǎng)友

臨近春節(jié),項(xiàng)目都結(jié)束了,都等著回家過年了。下面是小編給大家研究數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),鏈表算是經(jīng)常用到的一種數(shù)據(jù)結(jié)構(gòu)了,現(xiàn)將自己的實(shí)現(xiàn)展示如下,歡迎大神賜教。

第一個(gè)版本,沒有最后一個(gè)節(jié)點(diǎn),每次從根節(jié)點(diǎn)開始遍歷

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)){//移除這個(gè)元素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;}}@Overridepublic 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)+"]";}/**節(jié)點(diǎn)信息*/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;}} }

第二個(gè)版本,有了最后一個(gè)節(jié)點(diǎn)

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){//如果最后一個(gè)節(jié)點(diǎn)是空的則這個(gè)鏈表就是空的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)){//移除這個(gè)元素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;}}@Overridepublic 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)+"]";}/**節(jié)點(diǎn)信息*/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;}}}

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

以上所述是小編給大家介紹的Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)的相關(guān)知識(shí),希望對(duì)大家有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙川县| 乌拉特前旗| 揭西县| 邳州市| 新津县| 乐陵市| 呼图壁县| 嘉禾县| 孝义市| 大悟县| 林周县| 县级市| 台东县| 阿瓦提县| 栾城县| 大荔县| 称多县| 西林县| 香河县| 城步| 郧西县| 常山县| 稷山县| 红原县| 万安县| 开鲁县| 靖远县| 尤溪县| 九龙县| 沙湾县| 富锦市| 伊春市| 正阳县| 恩平市| 格尔木市| 天长市| 合江县| 合山市| 乐都县| 洛阳市| 湘阴县|