Java data structure implementation of two-way linked list example _java

Source: Internet
Author: User
Tags static class

Copy Code code as follows:

/**
* Realization of bidirectional linked list
* @author Skip
* @version 1.0
*/
public class Doublenodelist<t> {
Node class
private Static Class node<t>{
Node<t> perv; Front node
Node<t> Next; Post node
T data; Data

Public Node (T-t) {
This.data = t;
}
}
Private node<t> head; Head node
Private node<t> last; Tail node
Private node<t> Other; Standby node holds temporary operations
private int length; Chain list length

/**
* Non-parametric construction
*/
Public Doublenodelist () {
Head = new node<t> (null);
last = head;
length = 0;
}

/**
* Create a node when initializing
* @param data
*/
Public doublenodelist (T data) {
Head = new node<t> (data);
last = head;
length = 1;
}

/**
* Add a Node
* @param data added
*/
public void Add (T data) {
if (IsEmpty ()) {
Head = new node<t> (data);
last = head;
length++;
}else{
Tail interpolation method
other = new node<t> (data);
Other.perv = Last;
Last.next = other;
last = other;
length++;
}
}

/**
* Insert a node after the specified data
* @param data specified by
* @param data inserted by InsertData
* @return Insert successfully returned true, False returned unsuccessfully
*/
public boolean Addafert (t data, T InsertData) {
other = head;
While (the other!= null) {
if (other.data.equals (data)) {
node<t> T = new node<t> (insertdata);
T.perv = other;
T.next = Other.next;
Other.next = t;
To determine whether to add a node after the last node
if (t.next==null) {
last = t;
}
length++;
return true;
}
other = Other.next;
}
return false;
}

/**
* Insert a node before the specified data
* @param data specified by
* @param data inserted by InsertData
* @return Insert successfully returned true, False returned unsuccessfully
*/
public boolean Addbefore (t data, T InsertData) {
other = head;
While (the other!= null) {
if (other.data.equals (data)) {
node<t> T = new node<t> (insertdata);
T.perv = Other.perv;
T.next = other;
Other.perv.next = t;
length++;
return true;
}
other = Other.next;
}
return false;
}

/**
* Get the data at the index
* @param index
* @return Data
*/
Public T get (int index) {
if (Index>length | | index<0) {
throw new Indexoutofboundsexception ("Index Out of Bounds:" +index);
}
other = head;
for (int i=0;i<index;i++) {
other = Other.next;
}
return other.data;
}

/**
* New value replaces old value
* @return Success is true, false is not found
*/
public Boolean set (T oldvalue,t newvalue) {
other = head;
while (Other!=null) {
if (Other.data.equals (OldValue)) {
Other.data = newvalue;
return true;
}
other = Other.next;
}
return false;
}

/**
* Remove the specified element
* @param the elements that data needs to be removed
* @return does not exist for false, success is true
*/
public boolean remove (T data) {
other = head;
While (the other!= null) {
if (other.data.equals (data)) {
Other.perv.next = Other.next;
length--;
return true;
}
other = Other.next;
}
return false;
}

/**
* Whether this element is included in the list
* @return is included as true and does not contain false
*/
Public Boolean contains (T data) {
other = head;
While (the other!= null) {
if (other.data.equals (data)) {
return true;
}
other = Other.next;
}
return false;
}

/**
* Get the last node data
* @return data for the last node
*/
Public T GetLast () {
return last.data;
}

/**
* Get data for the first node
* @return data for the first node
*/
Public T GetFirst () {
return head.data;
}

/**
* Get the length of the linked list
* @return Length
*/
public int GetSize () {
return length;
}

/**
* is an empty list
* @return Empty list is true, Non-empty list is False
*/
public Boolean IsEmpty () {
return length==0;
}

/**
* Clear the list
*/
public void Clear () {
head = NULL;
length = 0;
}

/**
* All nodes in the output list
*/
public void Printlist () {
if (IsEmpty ()) {
System.out.println ("Empty list");
}else{
other = head;
for (int i=0;i<length;i++) {
System.out.print (other.data+ "");
other = Other.next;
}
System.out.println ();
}
}
}

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.