Java implementation bidirectional linked list (two versions) _java

Source: Internet
Author: User
Tags stringbuffer

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.