双向链表
class DoubleLinkedList{
DoubleDataNode head = new DoubleDataNode();
public void add(DoubleDataNode node){
DoubleDataNode tmp = head;
while(true){
if(tmp.next == null){
tmp.next = node;
node.pre = tmp;
break;
}
tmp = tmp.next;
}
}
public boolean update(DoubleDataNode node){
DoubleDataNode tmp = head;
while(true){
if(tmp.next == null){
return false;
}
if(tmp.next.no == node.no){
node.next = tmp.next.next;
tmp.next.next.pre = node;
tmp.next = node;
node.pre = tmp;
return true;
}
tmp = tmp.next;
}
}
public boolean delete(int no){
DoubleDataNode tmp = head.next;
while (true){
if(tmp == null){
return false;
}
if(tmp.no == no){
if(tmp.next == null){
tmp.next.pre = tmp.pre;
}
tmp.pre.next = tmp.next;
return true;
}
tmp = tmp.next;
}
}
public void show(){
DoubleDataNode tmp = head;
while(true){
if(tmp.next == null){
break;
}
System.out.println(tmp.next);
tmp = tmp.next;
}
}
}
class DoubleDataNode<T>{
public T data;
public int no;
public DoubleDataNode next;
public DoubleDataNode pre;
public DoubleDataNode(int no,T data){
this.no = no;
this.data = data;
}
public DoubleDataNode(){}
@Override
public String toString() {
return "DoubleDataNode{ no=" + no + ", data=" + data.toString() + '}';
}
}