复制代码 代码如下:
public class DoubleNodeList
//节点类
private static class Node
Node
Node
T data; //数据
public Node(T t){
this.data = t;
}
}
private Node
private Node
private Node
private int length; //链表长度
public DoubleNodeList(){
head = new Node
last = head;
length = 0;
}
public DoubleNodeList(T data){
head = new Node
last = head;
length = 1;
}
public void add(T data){
if(isEmpty()){
head = new Node
last = head;
length++;
}else{
//尾插法
other = new Node
other.perv = last;
last.next = other;
last = other;
length++;
}
}
public boolean addAfert(T data , T insertData){
other = head;
while(other != null){
if(other.data.equals(data)){
Node
t.perv = other;
t.next = other.next;
other.next = t;
//判断是否在最后一个节点后添加节点
if(t.next==null){
last = t;
}
length++;
return true;
}
other = other.next;
}
return false;
}
public boolean addBefore(T data, T insertData){
other = head;
while(other != null){
if(other.data.equals(data)){
Node
t.perv = other.perv;
t.next = other;
other.perv.next = t;
length++;
return true;
}
other = other.next;
}
return false;
}
public T get(int index){
if(index>length || index<0){
throw new IndexOutOfBoundsException("索引越界:"+index);
}
other = head;
for(int i=0;i
}
return other.data;
}
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;
}
public boolean remove(T data){
other = head;
while(other != null){
if(other.data.equals(data)){
other.perv.next = other.next;
length--;
return true;
}
other = other.next;
}
return false;
}
public boolean contains(T data){
other = head;
while(other != null){
if(other.data.equals(data)){
return true;
}
other = other.next;
}
return false;
}
public T getLast(){
return last.data;
}
public T getFirst(){
return head.data;
}
public int getSize(){
return length;
}
public boolean isEmpty(){
return length==0;
}
public void clear(){
head = null;
length = 0;
}
public void printList(){
if(isEmpty()){
System.out.println("空链表");
}else{
other = head;
for(int i=0;i
other = other.next;
}
System.out.println();
}
}
}



