数据结构:链表底层实现
链表特点
数据存储在节点中
//内部类
private class Node{
public E e;
//next存储下一个节点的地址
public Node next;
public Node(E e,Node next){
this.e = e;s
this.next = next;
}
优缺点
优点:真正的动态,不需要处理固定容量的问题。
缺点:丧失了随机访问的能力。
数组与链表的对比
数组最好用于索引有语意的情况。可以直接通过数组下标访问某一个值。
最大的优点:支持快速查询。
链表不适用于索引有语意的情况。访问某一个值需要从头开始遍历。
最大的优点:动态。
链表头节点问题:
添加元素和删除元素
因为添加元素和删除元素都是需要找到待操作元素位置的前一个元素,所以对于这种情况,在链表的第一个节点,即头节点需要做相应的特殊处理。
简化操作的方法:添加一个虚拟头节点。
可以在链表头添加一个虚拟头节点dummyHead,该节点不存储任意元素,dunmmyHead.next = head.
浪费一个空间,但简化了操作。这样,链表中每一个节点都有前一个节点。
链表代码:
整体结构
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; //不允许用户直接修改size变量,size由LinkedList内部进行维护
public LinkedList(){
dummyhead = new Node(null,null);
size = 0;
}
//获取链表中的元素个数
public int getSize(){
return size;
}
//返回链表是否为空
public boolean isEmpty(){
return size == 0;
}
}
添加操作
// 在链表的index(0-based)位置添加元素e 注意:索引是从0开始
public void add(int index,E e){
if(index < 0 || index > size )
throw new IllegalArgumentException("Add failde. Illegal idnex.");
Node prev = dummyhead;
for(int i = 0; i < index ; i ++ )
prev = prev.next;
// Node node = new Node(e);
// node.next = prev.next;
// prev.next = node;
//上面三行相当于下面这一行,简化代码
prev.next = new Node(e,prev.next);
size ++;
}
// 在链表头添加新的元素e
public void addFirst(E e){
// Node node = new Node(e);
// node.next = head;
// head = node;
// head = new Node(e,head); //一行代码完成三行代码表达的意思
// size ++;
add(0,e);
}
// 在链表末尾添加新的元素e
public void addLast(E e){
add(size,e);
}
链表的遍历,查询以及修改
关键:找到要添加的节点的前一个节点
// 获得链表的第index(0-based)个位置的元素
public E get(int index){
if(index < 0 || index >= size )
throw new IllegalArgumentException("Get failde. Illegal idnex.");
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 );
}
// 修改链表的第index(0-based)个位置的元素为e
// 在链表中不是一个常用的操作,练习用:)
public void set(int index,E e){
if(index < 0 || index >= size )
throw new IllegalArgumentException("Updated failde. Illegal idnex.");
Node cur = dummyhead.next;
for(int i = 0; i <= index; i ++ )
cur = cur.next;
cur.e = e;
}
// 查找链表中是否有元素e
public boolean contains(E e){
Node cur = dummyhead.next;
// for(int i = 0; i <= size; i ++){
// if(cur.e.equals(e))
// return true;
// cur = cur.next;
// }
// return false;
while (cur != null){
if(cur.e.equals(e))
return true;
cur = cur.next;
}
return false;
}
删除操作
关键:找到要删除的节点的前一个节点
//从链表中删除index(0-based)位置的元素,返回删除的元素
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 delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
return delNode.e;
}
// 从链表中删除第一个元素,返回删除的元素
public E removeFirst(){
return remove(0);
}
// 从链表中删除最后一个元素,返回删除的元素
public E removeLast(){
return remove(size - 1);
}
重写toString方法,方便打印
@Override
public String toString(){
StringBuilder res = new StringBuilder();
// Node cur = dummyhead.next;
// while(cur != null){
// res.append(cur + "->");
// cur = cur.next;
// }
for(Node cur = dummyhead.next; cur != null; cur = cur.next)
res.append(cur + "->");
res.append("NULL");
return res.toString();
}