复制代码 代码如下:
public class NodeList
private static class Node
E data; // 节点上的数据
Node
Node(E e) {
this.data = e;
this.next = null;
}
}
private Node
private Node
private Node
private int length = 0; // 节点数量
public NodeList() {
// 默认节点为空
this.head = new Node
}
public NodeList(E data) {
this.head = new Node
this.last = head;
length++;
}
public void add(E data) {
if (isEmpty()) {
head = new Node
last = head;
length++;
} else {
Node
last.next = newNode;
last = newNode;
}
}
public E get(int index){
if(index<0 || index>length){
throw new IndexOutOfBoundsException("索引越界:"+index);
}
other = head;
for(int i=0;i
}
return other.data;
}
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;
}
public boolean add(E data, E insertData) {
other = head;
while (other != null) {
if (other.data.equals(data)) {
Node
Node
newNode.next = temp;
other.next = newNode;
length++;
return true;
}
other = other.next;
}
return false;
}
public boolean contains(E data){
other = head;
while(other!=null){
if(other.data.equals(data)){
return true;
}
other = other.next;
}
return false;
}
public boolean remove(E data){
other = head;
Node
while(other!=null){
if(other.data.equals(data)){
temp.next = other.next;
length--;
return true;
}
temp = other;
other = other.next;
}
return false;
}
public boolean isEmpty() {
return length == 0;
}
public void clear() {
this.head = null;
this.length = 0;
}
public void printlink() {
if(isEmpty()){
System.out.println("空链表");
}else{
other = head;
while (other != null) {
System.out.print(other.data);
other = other.next;
}
System.out.println();
}
}
}



