在java的双向链表中,pre存放的是上一个数字数据域的地址,next存放的是下一个数组数据域的地址
class Link<T extends Comparable<T>> { private Entry<T> head; public Link() { head = new Entry<>(null, null, null); } public void insertHead(T val){ Entry<T> node=new Entry<>(val,this.head,this.head.next); this.head.next=node; if(node.next!=null){ node.next.pre=node; } } public void insertTail(T val){ Entry<T> cur=this.head; while(cur.next!=null){ cur=cur.next; } cur.next=new Entry<>(val,cur,cur.next); } /** * 删除所有值为val的节点 */ public void remove(T val){ Entry<T> cur=this.head.next; while(cur!=null){ if(cur.data.compareTo(val)==0){ cur.pre.next=cur.next; } if(cur.next!=null){ cur.next.pre=cur.pre; } } } public void show(){ Entry<T> cur=this.head.next; while (cur.next!=null){ System.out.println(cur.data+" "); cur=cur.next; } System.out.println(); } static class Entry<T> { T data; Entry<T> pre; Entry<T> next; public Entry(T data, Entry<T> pre, Entry<T> next) { this.data = data; this.pre = pre; this.next = next; } } }