По мере приближения весеннего фестиваля проект закончился, и мы все ждем, чтобы вернуться домой на новый год. Ниже приведены соответствующие знания редактора, изучающего структуру данных для вас. Связанный список - это структура данных, которая часто используется. Теперь я покажу свою собственную реализацию следующим образом. Добро пожаловать в великий мастер за советом.
Первая версия не имеет последнего узла, и она начинает каждый раз проходить от корневого узла
Общедоступный класс 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); 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); LinkedList <e> remove (e e) {node 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;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)+"]";}/** node node node node node node; Значение, узел pre, node next) {this.value = value; this.pre = pre; this.next = next;}}}Вторая версия имеет последний узел
открытый класс 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; et return vature 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;}} lst.next = new Node (e, lst, null); last.next; return this;}} public linkedlist <e> lst = head; if (lst == null) {бросить новое 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;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)+"]";}/** node node node node node node; Значение, узел pre, node next) {this.value = value; this.pre = pre; this.next = next;}}}Примечание. Ни одна из вышеупомянутых двух версий не рассматривает использование при многопоточном состоянии.
Выше приведено соответствующие знания о реализации двунаправленных связанных списков (две версии), введенные вам редактором. Я надеюсь, что это будет полезно для вас.