具体思想请参考Java构建单向链表一文
public class DoubleLinked<T> {
public class Node{
private Node prev;
private T t;
private Node next;
public Node(Node prev, T t, Node next){
this.prev = prev;
this.t = t;
this.next = next;
}
public Node(T t){
this.prev = null;
this.t = t;
this.next = null;
}
public Node(){
}
public T get(){
return this.t;
}
public void set(T t){
this.t = t;
}
}
private Node head;
private Node tail;
private int size;
public DoubleLinked(){
this.head = null;
this.size = 0;
}
public int getSize(){
return this.size;
}
public Node getPrevNodeByNode(Node node){
if(node.prev == null){
throw new NullPointerException("向前没有结点!");
}else{
return node.prev;
}
}
public Node getNextNodeByNode(Node node){
if(node.next == null){
throw new NullPointerException("向前没有结点!");
}else{
return node.next;
}
}
public Node getFirstNode(){
if(this.size == 0){
throw new NullPointerException("链表中不存在结点");
}else{
return this.head;
}
}
public Node getLastNode(){
if(this.size == 0){
throw new NullPointerException("链表中不存在结点");
}else{
return this.tail;
}
}
public Node getNode(int index){
if(index < 0 || index > this.size-1){
throw new IllegalArgumentException("index is error");
}
if(index == 0){
return this.head;
}
if(index == size-1){
return this.tail;
}
Node node = this.head;
for(int i=0; i<index; i++){
node = node.next;
}
return node;
}
public void updateFirstNode(T t){
if(this.size == 0){
System.out.println("没有可以修改的结点!");
return;
}
this.getFirstNode().set(t);
}
public void updateLastNode(T t){
if(this.size == 0){
System.out.println("没有可以修改的结点!");
return;
}
this.getLastNode().set(t);
}
public void updateNodeByIndex(T t, int index){
if(index < 0 || index > this.size-1){
throw new IllegalArgumentException("index is error");
}
if(this.size == 0){
System.out.println("没有可以修改的结点!");
return;
}
if(index == 0){
this.updateFirstNode(t);
return;
}
if(index == this.size-1){
this.updateLastNode(t);
return;
}
this.getNode(index).set(t);
}
public void addFirst(T t){
Node node = new Node(t);
node.next = head;
this.head = node;
if(size == 0){
this.tail = node;
}
size++;
}
public void addLast(T t){
if(this.size == 0){
addFirst(t);
return;
}
Node node = this.head;
while(node.next != null){
node = node.next;
}
Node newNode = new Node(t);
node.next = newNode;
newNode.prev = node;
this.tail = newNode;
size++;
}
public void add(T t, int index){
if(index < 0 || index > this.size){
throw new IllegalArgumentException("index is error");
}
if(index == 0){
addFirst(t);
return;
}
if(index == this.size-1){
addLast(t);
return;
}
Node node = this.head;
for(int i=0; i<index-1; i++){
node = node.next;
}
Node newNode = new Node(t);
newNode.next = node.next;
newNode.prev = node;
node.next = newNode;
size++;
}
public void deleteFirst(){
if(this.size == 0){
System.out.println("没有可以删除的结点");
return;
}
if(this.size == 1){
this.head = null;
this.size = 0;
this.tail = null;
return;
}
this.head = head.next;
head.prev = null;
size--;
}
public void deleteLast(){
if(this.size == 0){
System.out.println("没有可以删除的结点");
return;
}
if(this.size == 1){
deleteFirst();
return;
}
Node temp = this.getPrevNodeByNode(this.getLastNode());
temp.next = null;
this.tail = temp;
this.size--;
}
public void delete(int index){
if(index < 0 || index > this.size-1){
throw new IllegalArgumentException("index is error");
}
if(index == 0){
this.deleteFirst();
return;
}
if(index == this.size-1){
this.deleteLast();
return;
}
Node node = this.getNode(index);
Node prev = this.getPrevNodeByNode(node);
Node next = this.getNextNodeByNode(node);
prev.next = next;
next.prev = prev;
size--;
}
public void deleteNodeByNode(Node node){
if(this.head == node){
this.deleteFirst();
return;
}
if(this.tail == node){
this.deleteLast();
return;
}
Node prev = this.getPrevNodeByNode(node);
Node next = this.getNextNodeByNode(node);
prev.next = next;
next.prev = prev;
size--;
}
}