4-1 什麼是鏈表
爲什麼鏈表很重要
- 鏈表:真正的動態數據結構
- 最簡單的動態數據結構
- 更深入的理解引用(或者指針)
- 更深入的理解遞歸
- 輔助組成其他數據結構
鏈表
數據存儲在"節點(Node)中
class Node {
E e;
Node next;
}
優點:真正的動態,不需要處理固定容量的問題
缺點:喪失了隨機訪問的能力
數組與鏈表的對比
數組 |
鏈表 |
數組最好用於索引有語意的情況。scores[2] |
鏈表不適合索引有語意的情況 |
支持快速查詢 |
動態 |
public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
}
4-2 鏈表Linked List
在鏈表頭添加元素
public void addFirst(E e){
head = new Node(e, head);
size ++;
}
在鏈表中間和末尾添加元素
public void add(int index, E e){
if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
if(index == 0)
addFirst(e);
else{
Node prev = head;
for(int i = 0 ; i < index - 1 ; i ++)
prev = prev.next;
prev.next = new Node(e, prev.next);
size ++;
}
}
public void addLast(E e){
add(size, e);
}
4-3 使用鏈表的虛擬頭結點
public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
@Override
public String toString(){
return e.toString();
}
}
private Node dummyHead;
private int size;
public LinkedList(){
dummyHead = new Node();
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(int index, E e){
if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++)
prev = prev.next;
prev.next = new Node(e, prev.next);
size ++;
}
public void addFirst(E e){
add(0, e);
}
public void addLast(E e){
add(size, e);
}
}
4-4 鏈表的遍歷,查詢和修改
public E get(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
return cur.e;
}
public E getFirst(){
return get(0);
}
public E getLast(){
return get(size - 1);
}
public void set(int index, E e){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
cur.e = e;
}
public boolean contains(E e){
Node cur = dummyHead.next;
while(cur != null){
if(cur.e.equals(e))
return true;
cur = cur.next;
}
return false;
}
4-5 從鏈表中刪除元素
public E remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal.");
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++)
prev = prev.next;
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size --;
return retNode.e;
}
public E removeFirst(){
return remove(0);
}
public E removeLast(){
return remove(size - 1);
}
public void removeElement(E e){
Node prev = dummyHead;
while(prev.next != null){
if(prev.next.e.equals(e))
break;
prev = prev.next;
}
if(prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
}
}
鏈表的時間複雜度分析
添加操作 |
時間複雜度 |
addLast(e) |
O(n) |
addFirst(e) |
O(1) |
add(index,e) |
O(n/2)=O(n) |
綜合 |
O(n) |
刪除操作 |
時間複雜度 |
removeLast(e) |
O(n) |
removeFirst(e) |
O(1) |
remove(index,e) |
O(n/2)=O(n) |
綜合 |
O(n) |
修改操作 |
時間複雜度 |
set(index,e) |
O(n) |
查找操作 |
時間複雜度 |
get(index) |
O(n) |
contains(e) |
O(n) |
4-6 使用鏈表實現棧
public class LinkedListStack<E> implements Stack<E> {
private LinkedList<E> list;
public LinkedListStack(){
list = new LinkedList<>();
}
@Override
public int getSize(){
return list.getSize();
}
@Override
public boolean isEmpty(){
return list.isEmpty();
}
@Override
public void push(E e){
list.addFirst(e);
}
@Override
public E pop(){
return list.removeFirst();
}
@Override
public E peek(){
return list.getFirst();
}
4-7 帶有尾指針的鏈表:使用鏈表實現隊列
public class LinkedListQueue<E> implements Queue<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
@Override
public String toString(){
return e.toString();
}
}
private Node head, tail;
private int size;
public LinkedListQueue(){
head = null;
tail = null;
size = 0;
}
@Override
public int getSize(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0;
}
@Override
public void enqueue(E e){
if(tail == null){
tail = new Node(e);
head = tail;
}
else{
tail.next = new Node(e);
tail = tail.next;
}
size ++;
}
@Override
public E dequeue(){
if(isEmpty())
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
Node retNode = head;
head = head.next;
retNode.next = null;
if(head == null)
tail = null;
size --;
return retNode.e;
}
@Override
public E getFront(){
if(isEmpty())
throw new IllegalArgumentException("Queue is empty.");
return head.e;
}
}