释义见单链表及其常见操作简单代码实现:单链表
package alo;
public class 双链表_DoubleLinkedList {
public static void main(String[] args){
DoubleLinkedList cos=new DoubleLinkedList();
cos.add(1);
cos.add(2);
cos.remove(2);
cos.remove(0);
cos.showNode();
}
}
class DNode{
public int value;
DNode pre;
DNode next;
public DNode(int value){
this.value=value;
}
}
class DoubleLinkedList{
DNode head=new DNode(0);
public DoubleLinkedList(){}
public void add(int value){
DNode indexDNode=head;
while(true) {
if(indexDNode.next==null){
indexDNode.next = new DNode(value);
indexDNode.next.pre =indexDNode;
break;
}
indexDNode=indexDNode.next;
}
}
public void showNode(){
DNode indexDNode=head;
while(true){
if(indexDNode.next==null){
System.out.println("DNode.value="+indexDNode.value);
break;
}
System.out.println("DNode.value="+indexDNode.value);
indexDNode=indexDNode.next;
}
}
public void remove(int num){
DNode index=head;
if(num==0) {
System.out.println("头节点不能删除");
return;
}
while(true){
if(index.next==null) {
index.pre.next=null;
break;
}
if(index.value==num){
index.pre.next=index.next;
index.next.pre =index.pre;
}
index=index.next;
}
}
}