春のフェスティバルが近づくと、プロジェクトは終わり、私たちは皆、新年のために家に帰るのを待っています。以下は、あなたのためにデータ構造を研究している編集者に関する関連する知識です。リンクリストは、よく使用されるデータ構造です。次に、次のように自分の実装を表示します。アドバイスのために偉大なマスターへようこそ。
最初のバージョンには最後のノードがなく、毎回ルートノードから移動し始めます
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){e e){head.pre = new; 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 = nult = nunt this;}} public linkedlist <e> remove(e e){node lst = head; if(lst == null){throw new nullpointerexception( "linkedlist is empty。");} else {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;} lst = lst.next;} return this;}}@over ridepublic string buff buff( StringBuffer( "["); node lst = this.head; while(lst!= null){buff.append(lst.value+"、"); lst = lst.next;} return buff.substring(0、buff.length.substring(0、ubl.length() - 1)+"]"] ";}/** node node node {public node epubl e value、node pre、node next){this.value = value; this.pre = pre; this.next = next;}}}}}}}2番目のバージョンには最後のノードがあります
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(){(last == null){return null;} return last.value;} reling <e> value;} 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.head = this.last; return this;} else {while(true){if(lst.next == null){// break;} else {lst = lst.next;}} node = new node(e、lst、null); last = lst.next; return this;}} public linkedlist <e e lst = head; if(lst == null){throw new nullpointerexception( "linkedlistは空です。");} 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;} lst = lst.next;} return this;}}@over ridepublic string buff buff( StringBuffer( "["); node lst = this.head; while(lst!= null){buff.append(lst.value+"、"); lst = lst.next;} return buff.substring(0、buff.length.substring(0、ubl.length() - 1)+"]"] ";}/** node node node {public node epubl e value、node pre、node next){this.value = value; this.pre = pre; this.next = next;}}}}}}}注:上記の2つのバージョンのいずれも、マルチスレッドでの使用を考慮していません。
上記は、編集者が紹介した双方向リンクリスト(2つのバージョン)の実装に関する関連する知識です。それがあなたに役立つことを願っています。