Saat Festival Musim Semi mendekat, proyek ini sudah berakhir dan kami semua menunggu untuk pulang untuk Tahun Baru. Di bawah ini adalah pengetahuan yang relevan dari editor yang mempelajari struktur data untuk Anda. Daftar tertaut adalah struktur data yang sering digunakan. Sekarang saya akan menunjukkan implementasi saya sendiri sebagai berikut. Selamat datang di The Great Master untuk nasihat.
Versi pertama tidak memiliki simpul terakhir, dan mulai melintasi dari node root setiap saat
Public Class LinkedList <E> {private node head; public linkedList () {} public e getFirst () {if (head == null) {return null;} return head.value;} tautan publik <e> addFirst (e e) {head.pre = new node (e, null, null, null, null); addNode (e e) {node lst = head; if (lst == null) {this.head = nod node (e, null, null); return ini;} else {while (true) {if (lst.next == null) {break;} else {lst = lst.next;}} lst. ini;}} public LinkedList <E> hapus (e e) {node lst = head; if (lst == null) {lempar nullpointerException baru ("LinkedList kosong.");} else {while (true) {if (e.equals (lst.value)) {// hapus elemen ini if (lst.pre! = null) {lst.pre.next = lst.next;} if (lst.next! = null) {lst.next.pre = lst.pre;} lst = null; break;} lts = lst.next;} return this;}}@overlridepublic = Lst.nexted; StringBuffer("[");Node lst=this.head;while(lst!=null){buff.append(lst.value+",");lst=lst.next;}return buff.substring(0, buff.length()-1)+"]";}/**Node information*/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;}}}Versi kedua memiliki simpul terakhir
kelas publik LinkedList <E> {private node head; node pribadi terakhir; public linkedlist () {} public e getFirst () {if (head == null) {return null;} return head.value;} public e getLast () {if (last == null) {return null;} return last. E) {head.pre = node baru (e, null, head); head = head.pre; kembalikan ini;} linkedlist publik <e> addNode (e e) {node lst = terakhir; if (lst == null) {// jika node terakhir kosong, daftar tautan ini kosong ini. 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) {lempar new nullpointerException ("LinkedList kosong.");} else {while (true) {if (e.equals (lst.value)) {// hapus elemen ini if (lst.pre! = null) {lst.pre.next = lst.next;} if (lst.next! = null) {lst.next.pre = lst.pre;} lst = null; break;} lts = lst.next;} return this;}}@overlridepublic = Lst.nexted; StringBuffer("[");Node lst=this.head;while(lst!=null){buff.append(lst.value+",");lst=lst.next;}return buff.substring(0, buff.length()-1)+"]";}/**Node information*/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;}}}Catatan: Tak satu pun dari dua versi di atas mempertimbangkan penggunaan di bawah multithreading.
Di atas adalah pengetahuan yang relevan tentang menerapkan daftar ditautkan dua arah (dua versi) yang diperkenalkan kepada Anda oleh editor. Saya harap ini akan membantu Anda.