最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年02月06日 16:44:28   投稿:mrr  
這篇文章主要介紹了Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)的相關(guān)資料,需要的朋友可以參考下

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

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

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;
}
}
@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)+"]";
}
/**節(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;
}
}
@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)+"]";
}
/**節(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è)版本都沒(méi)有考慮在多線程下使用的情況。

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

相關(guān)文章

最新評(píng)論

永吉县| 始兴县| 河源市| 揭阳市| 津市市| 鸡西市| 孝感市| 安图县| 吉安市| 靖州| 新田县| 左贡县| 康乐县| 芮城县| 微博| 高阳县| 丹东市| 葫芦岛市| 金川县| 元朗区| 马关县| 东莞市| 嘉定区| 土默特左旗| 城步| 余干县| 沁源县| 华阴市| 洛阳市| 涞源县| 洛扎县| 阿城市| 色达县| 阳泉市| 仙居县| 巫山县| 隆林| 五原县| 长沙市| 蓬溪县| 祁连县|