A medida que se acerca el festival de primavera, el proyecto ha terminado y todos estamos esperando ir a casa para el año nuevo. A continuación se muestra el conocimiento relevante del editor que estudia la estructura de datos para usted. La lista vinculada es una estructura de datos que a menudo se usa. Ahora mostraré mi propia implementación de la siguiente manera. Bienvenido al Gran Maestro por consejo.
La primera versión no tiene el último nodo, y comienza a atravesar desde el nodo raíz cada vez
public class LinkedList <E> {private Node Head; public LinkedList () {} public E getFirst () {if (head == null) {return null;} returns.value;} public LinkedList <E> addfirst (e e) {head.pre = new Node (e, null, head = head = head.pre; return esto;} 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, l, lst, n. esto;}} public LinkedList <E> remoque (e e) {nodo lst = head; if (lst == null) {tire new nullPointerException ("La lista de enlaces está vacía.");} else {while (true) {if (eequals (lst.value)) {// elimina este elemento if (lst.Pre! = null) {lst.Pre.next = lst.next;} if (lst.next! = null) {lst.next.pre = lst.pre ;pre;} lst = null; break;} lst = lst.Next;} return this;}}}@overidEpublicte string tostring () {StringBefFerBuFferBufferBuFferBuFferBuFferBeFferBeFeFerBeFeFerFeFerBeFUfferBeFeFerBeFeFerBeFeFER StringBuffer ("[["); nodo lst = this.head; while (lst! = Null) {buff.append (lst.value+","); lst = lst.next;} return buff.substring (0, buff.length ()-1)+"]";}/** Información de la clase de núcleo*/Nodo privado {Public Evalod valor, nodo pre, nodo Next) {this.value = value; this.pre = pre; this.next = next;}}}La segunda versión tiene el último nodo
public class LinkedList <E> {private Node Head; private nodo Last; public LinkedList () {} public e getFirst () {if (head == null) {return null;} return.value;} public e getLast () {if (Last == null) {return null;} return Last.value;} public linglist <e> (e> (e> (e> andfirster 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) {// Si el último nodo está vacío, esta lista de enlaces está vacía este.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.nex lst = head; if (lst == null) {tire nueva nullPointerException ("La lista de enlaces está vacía");} else {while (true) {if (e.equals (lst.value)) {// elimina este elemento if (lst.Pre! = null) {lst.Pre.next = lst.next;} if (lst.next! = null) {lst.next.pre = lst.pre ;pre;} lst = null; break;} lst = lst.Next;} return this;}}}@overidEpublicte string tostring () {StringBefFerBuFferBufferBuFferBuFferBuFferBeFferBeFeFerBeFeFerFeFerBeFUfferBeFeFerBeFeFerBeFeFER StringBuffer ("[["); nodo lst = this.head; while (lst! = Null) {buff.append (lst.value+","); lst = lst.next;} return buff.substring (0, buff.length ()-1)+"]";}/** Información de la clase de núcleo*/Nodo privado {Public Evalod valor, nodo pre, nodo Next) {this.value = value; this.pre = pre; this.next = next;}}}Nota: Ninguna de las dos versiones anteriores considera el uso en múltiples lecturas.
Lo anterior es el conocimiento relevante sobre la implementación de listas vinculadas bidireccionales (dos versiones) presentadas por el editor. Espero que te sea útil.