Copy Code code as follows:
/**
* One-way linked list
*
*/
public class Nodelist<e> {
private static Class Node<e> {//Node class
E data; Data on a node
Node<e> Next; Point to next node
Node (e e) {
This.data = e;
This.next = null;
}
}
Private node<e> head; The head node of a linked list
Private node<e> last; End node of a linked list
Private node<e> other = null;
private int length = 0; Number of nodes
/**
* Non-parametric construction method
*/
Public NodeList () {
The default node is empty
This.head = new node<e> (null);
}
/**
* Create a node when initializing
*
* @param data
* Data
*/
Public nodelist (E data) {
This.head = new node<e> (data);
This.last = head;
length++;
}
/**
* Add a node (trailing interpolation method)
*
* @param data
* Data
*/
public void Add (E data) {
if (IsEmpty ()) {
Head = new node<e> (data);
last = head;
length++;
} else {
node<e> NewNode = new node<e> (data);
Last.next = NewNode;
last = NewNode;
}
}
/**
* Get data at index (index input error throws out of bounds)
* @param index
* @return Data at index
*/
Public E get (int index) {
if (index<0 | | index>length) {
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 (E oldvalue,e newvalue) {
other = head;
while (Other!=null) {
if (Other.data.equals (OldValue)) {
Other.data = newvalue;
return true;
}
other = Other.next;
}
return false;
}
/**
* Inserts an element after the specified element
*
* @param data
* The specified element
* @param insertdata
* Elements that need to be inserted
* @return false to Element not found, true to insert success
*/
Public boolean Add (e data, E InsertData) {
other = head;
While (the other!= null) {
if (other.data.equals (data)) {
node<e> NewNode = new node<e> (insertdata);
node<e> temp = Other.next;
Newnode.next = temp;
Other.next = NewNode;
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 (E data) {
other = head;
while (Other!=null) {
if (other.data.equals (data)) {
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 (E data) {
other = head;
node<e> temp = head; A temporary variable used to save the previous node
while (Other!=null) {
if (other.data.equals (data)) {
Temp.next = Other.next;
length--;
return true;
}
temp = other;
other = Other.next;
}
return false;
}
/**
* Determine if the linked list is empty
*
* @return Null is True, Non-null is False
*/
public Boolean IsEmpty () {
return length = = 0;
}
/**
* Clear the list
*/
public void Clear () {
This.head = null;
this.length = 0;
}
/**
* Output ALL nodes
*/
public void Printlink () {
if (IsEmpty ()) {
System.out.println ("Empty list");
}else{
other = head;
While (the other!= null) {
System.out.print (Other.data);
other = Other.next;
}
System.out.println ();
}
}
}