雙向鏈表相對於單向鏈表的優點:
1、單向鏈表在需要對列表尾部進行操作時,需要通過遍歷的方式獲取尾部的結點,很浪費時間
2、對於需要頻繁使用deleteFromTail()和addToTail()方法的程序,程序效率會非常低
3、雙向鏈表存儲了每個結點前面的結點,在對尾部進行操作時,能非常方便的獲取尾部的結點信息,方便對尾部進行操作,大大提高了程序效率
4、但是,雙向鏈表比單向鏈表多存儲了一個對象的引用,空間複雜度更高
下方是Java代碼:
package com.DoublyLinkedList;
//雙向鏈表類
public class DoublyLinkedList {
Node head; //鏈表頭結點引用
Node tail; //鏈表尾結點引用
int length; //鏈表長度
//構造函數
public DoublyLinkedList(){
head = tail = null;
length = 0;
}
//判斷鏈表是否爲空
int isEmpty(){
return head == null?1:0;
}
//判斷鏈表是否包含某個值
int contains(int value){
//遍歷鏈表查找值
for (Node now=head; now!=null; now=now.next)
if (now.value == value)
return 1;
return 0;
}
//向鏈表頭添加結點
void addToHead(int value){
//如果鏈表爲空,直接添加
if (head == null)
head = tail = new Node(value, null, null);
else
//鏈表不爲空,添加結點,更新head
head = head.pre = new Node(value, null, head);
length++;
}
//向鏈表尾添加結點
void addToTail(int value){
//如果鏈表爲空,直接添加
if (head == null)
head = tail = new Node(value, null, null);
//鏈表不爲空,添加結點,更新tail
else{
tail = tail.next = new Node(value, tail, null);
}
length++;
}
//向指定下標添加結點
void addNode(int value, int index){
//如果指定下標不合法,直接返回
if (index > length+1 || index <= 0) return ;
//如果鏈表爲空,且指定下標不是1,直接返回
if (head==null && index!=1)
return ;
//如果鏈表爲空,需要新加入結點
if (head==null && index==1){
head = tail = new Node(value, null, null);
}
//如果鏈表不爲空,需要在頭部加入結點
else if (index==1)
head = head.pre = new Node(value, null, head);
//鏈表不爲空,在鏈表中加入結點
else {
int cnt = 0;
Node aheadOfAdd=head;
//找到要加入結點的前一個結點
for(cnt=1; cnt+1<index; cnt++)
aheadOfAdd = aheadOfAdd.next;
//如果在尾部之後加入結點,更新tail
if (aheadOfAdd == tail)
tail = tail.next = new Node(value, tail, null);
//否則直接加入
else
aheadOfAdd.next = aheadOfAdd.next.pre = new Node(value, aheadOfAdd, aheadOfAdd.next);
}
length++;
}
//刪除鏈表的第一個結點
int deleteFromHead(){
Node deletedNode = null;
//如果鏈表爲空,直接返回
if (head == null)
return -1;
//如果鏈表只有一個結點,刪除這個結點,更新head和tail
if (head == tail)
deletedNode = head;
//正常刪除
else{
deletedNode = head;
head = head.next;
head.pre = null;
}
length--;
return deletedNode.value;
}
//刪除鏈表的最後一個結點
int deleteFromTail(){
Node deletedNode = null;
//如果鏈表爲空,直接返回
if (head == null)
return -1;
//如果鏈表只有一個結點,刪除這個結點,更新head和tail
if (head == tail){
deletedNode = head;
head = tail = null;
}
//其他情況
else{
deletedNode = tail;
tail = tail.pre;
tail.next = null;
}
length--;
return deletedNode.value;
}
//按照給定下標刪除結點
int deleteNode(int index){
//如果給定index明顯不合法,直接返回
if (index<=0 || index>length)
return -1;
//要刪除的結點
Node deletedNode = head;
//如果鏈表爲空,直接返回
if (head == null) return -1;
//如果鏈表只有一個元素且要求刪除其他的元素,直接返回
if (head == tail && index != 1)
return -1;
//如果鏈表只有一個元素且要求刪除該元素,更新head和tail
if (head == tail && index == 1){
deletedNode = head;
head = tail = null;
}
//如果需要刪除鏈表頭結點
else if(index == 1){
deletedNode = head;
head = head.next;
head.pre = null;
}
//如果需要刪除鏈表尾結點
else if(index == length){
deletedNode = tail;
tail = tail.pre;
tail.next = null;
}
//其他情況
else{
int cnt;
for(deletedNode=head,cnt=1; cnt<index; cnt++, deletedNode=deletedNode.next);
deletedNode.pre.next = deletedNode.next;
deletedNode.next.pre = deletedNode.pre;
}
length--;
return deletedNode.value;
}
//打印鏈表
//從前向後和從後向前打印,確保前向和後向指針完全正確
void printSelf(){
System.out.print("DoublyLinkedList: [");
for (Node now=head; now!=null; now=now.next)
System.out.print(now.value + " ");
System.out.print("] Backside front: [");
for (Node now=tail; now!=null; now=now.pre)
System.out.print(now.value + " ");
System.out.print("]\n\t\t");
String str = String.format("Head: %d\t\ttail: %d\t\tLength: %d\n", head.value, tail.value, length);
System.out.print(str);
}
//測試函數
static public void main(String[] argv) {
DoublyLinkedList list = new DoublyLinkedList();
list.addToHead(100);list.printSelf();
list.addToHead(200);list.printSelf();
list.addToTail(1);list.printSelf();
list.addToTail(2);list.printSelf();
list.addToTail(3);list.printSelf();
list.deleteFromTail();list.printSelf();
list.deleteFromTail();list.printSelf();
list.addNode(10000, 1);list.printSelf();
System.out.print("\n\n");
//list.deleteNode(1);list.printSelf();
System.out.println("empty?:" + list.isEmpty());
System.out.println("123456?: " + list.contains(123456));
System.out.println("10000?: " + list.contains(10000));
}
}
//雙向鏈表結點類
class Node{
int value; //結點數據
Node pre; //前一個結點的引用
Node next; //後一個結點的引用
//構造函數1,最常用
public Node(int aValue){
value = aValue;
pre = null;
next = null;
}
//構造函數2
public Node(int aValue, Node aPre, Node aNext){
value = aValue;
pre = aPre;
next = aNext;
}
}