The code copy is as follows:
/**
* One-way linked list
*
*/
public class NodeList<E> {
private static class Node<E> { // Node class
E data; // Data on the node
Node<E> next; // Point to the next node
Node(E e) {
this.data = e;
this.next = null;
}
}
private Node<E> head; // header node of the linked list
private Node<E> last; // The tail node of the linked list
private Node<E> other = null;
private int length = 0; // Number of nodes
/**
* Non-parameter construction method
*/
public NodeList() {
// The default node is empty
this.head = new Node<E>(null);
}
/**
* Create a node during initialization
*
* @param data
* data
*/
public NodeList(E data) {
this.head = new Node<E>(data);
this.last = head;
length++;
}
/**
* Add a node (tail insertion 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 the data at the index (index input error throws an out-of-bounds exception)
* @param index index
* @return Data at index
*/
public E get(int index){
if(index<0 || index>length){
throw new IndexOutOfBoundsException("Index:"+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, not found as false
*/
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;
}
/**
* Insert an element after specifying the element
*
* @param data
* Specified element
* @param insertData
* Elements that need to be inserted
* @return false is element not found, true is insertion successful
*/
public boolean add(E data, E insertData) {
other = head;
while (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 linked list
* @return Included as true, not 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 data Elements that need to be removed
* @return does not exist and is true for success
*/
public boolean remove(E data){
other = head;
Node<E> temp = head;//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 whether the linked list is empty
*
* @return Empty is true, non-empty is false
*/
public boolean isEmpty() {
return length == 0;
}
/**
* Clear the link list
*/
public void clear() {
this.head = null;
this.length = 0;
}
/**
* Output all nodes
*/
public void printLink() {
if(isEmpty()){
System.out.println("Empty linked list");
}else{
other = head;
while (other != null) {
System.out.print(other.data);
other = other.next;
}
System.out.println();
}
}
}