Near the Spring Festival, the project is over, are waiting to go home for the new year. The following is a small set to the study of data structure of the relevant knowledge, linked list is often used in a data structure, is the realization of their own display as follows, welcome the great god Enlighten.
First version, no last node, every time from the root node to start traversing
public class Linkedlist<e> {private Node head; public LinkedList () {} public E GetFirst () {if (head==null) {return
Null
return head.value; Linkedlist<e> AddFirst (e e) {head.pre=new Node (E, NULL, head), Head=head.pre; Ist<e> AddNode (e E) {node lst=head; if (lst==null) {this.head=new node (e, NULL, NULL); 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 I s empty. ");}
else{while (true) {if (E.equals (Lst.value)) {//Remove this element 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 to 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) + "]";
/** 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;}} }
second version, with last node
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.P
Re=new Node (E, null, head);
Head=head.pre;
return this; Public linkedlist<e> AddNode (e E) {node lst=last; if (lst==null) {//If the last node is empty then the linked list is an empty 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 I s empty. ");}
else{while (true) {if (E.equals (Lst.value)) {//Remove this element 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 to 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) + "]";
/** 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;}} }
Note: The above two versions do not consider the use of multithreading under the situation.
The above is a small series to introduce the Java implementation of two-way linked list (two versions) of the relevant knowledge, hope to help you.