//E->anyType object private static class Node<E> { //数据项 private E data; //指向next节点的引用 private Node<E> next; //构造 public Node(E data, Node<E> next) { this.data = data; this.next = next; } }
private Node<E> head = null;
public void addFirst(E item) { head = new Node<E>(item, head); }
public void insertBefore(E key, E toInsert) { //若head为null,即链表为空,不存在目标节点 直接返回 if(head == null) return; //若head指向的链表第一个节点 就是目标节点,即要把新建节点插入链表首部 if(head.data.equals(key)) { addFirst(toInsert); return; } Node<E> prev = null; Node<E> curr = head; //curr定义了 指向当前节点,prev指向当前节点的上个节点。一直顺序查找,若找到目标节点(数据为key),则curr指向目标节点,prev指向了目标节点的上个节点。 while(curr != null && !curr.data.equals(key)) { prev = curr; curr = curr.next; } //若curr不为空,即找到目标节点,在curr和prev之间插入了新节点(数据为toInsert)。若curr为null,即没找到。 if(curr != null) prev.next = new Node<E>(toInsert, curr); }
public void insertAfter(E key, E toInsert) { Node<E> curr = head; //查找目标节点,找到即curr指向目标节点 while(curr != null && !curr.data.equals(key)) { curr = curr.next; } //curr不为null,即找到目标节点。在以后插入便可。 if(curr != null) curr.next = new Node<E>(toInsert, curr.next); }
public void addLast(E item) { if(head == null) { addFirst(item); } else { Node<E> tmp = head; while(tmp.next != null) tmp = tmp.next; tmp.next = new Node<E>(item, null); } }
public void remove(E key) { //head为null,即链表为空 if(head == null) throw new RuntimeException("linkedlist is null, cannot delete"); //链表第一个节点即目标节点,改变head指向下个节点就能够了。 if(head.data.equals(key)) { head = head.next; return; } Node<E> prev = null; Node<E> curr = head; //查找,若找到即curr指向目标节点,prev指向目标节点的上个节点 while(curr != null && !curr.data.equals(key)) { prev = curr; curr = curr.next; } //curr为null,即没找到 if(curr == null) throw new RuntimeException("cannot find your node, cannot delete"); //curr不为null,删除目标节点(即改变prev的next 指向curr的next便可) prev.next = curr.next; }
transient int size = 0; /** * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Node<E> first;//指向首部(第一个节点) /** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node<E> last;//指向尾部(最后一个节点) private static class Node<E> { //数据项 E item; //指向下个节点 Node<E> next; //指向上个节点 Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
/** * Links e as first element. */ private void linkFirst(E e) { //f指向first指向的节点。即f和first同时指向第一个节点。f在方法中标志 链表插入前的第一个节点 final Node<E> f = first; //建立新的节点,节点的prev指向null,next指向f(即链表插入前第一个节点) final Node<E> newNode = new Node<>(null, e, f); //first指向新节点(链表首部已改变) first = newNode; if (f == null) //若f为null,即插入前链表是空的,插入到新节点既是开始节点也是结束节点,因此last也指向了它 last = newNode; else //若是f不为空,则让f(插入前的第一个节点)的prev指向新节点就完成了。 f.prev = newNode; size++; modCount++; }
/** * Inserts element e before non-null Node succ. */ void linkBefore(E e, Node<E> succ) { // assert succ != null; //这里把succ称为目标节点。succ在这里是已经查找到并存在的,查找过程可参考单链表中相关代码。 //用pred指向 目标节点succ的上个节点。 final Node<E> pred = succ.prev; //新建节点,prev指向pred 即新建节点的prev指向目标节点的上个节点,next指向目标节点 final Node<E> newNode = new Node<>(pred, e, succ); //目标节点的prev指向 新建节点 succ.prev = newNode; if (pred == null) //若pred为null 是目标节点上个节点为null,即链表插入前只有一个节点,因此first会指向新建节点。 first = newNode; else //若pred不为null,目标节点的上个节点的next指向新节点 pred.next = newNode; size++; modCount++; }
/** * Links e as last element. */ void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }
/** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ public boolean remove(Object o) { //这里就是区分o是否为null,找到第一个指定element的节点,经过unlink删除。 if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } /** * Unlinks non-null node x. */ E unlink(Node<E> x) { // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; if (prev == null) { //prev为null 即x是首部(第一个节点),first指向next便可 first = next; } else { //prev不为null,x与prev之间关联即断开。prev的next指向next了,x的prev为null prev.next = next; x.prev = null; } //同prev处理相似,断开x与next联系 if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }
private Node<E> head = null; private Node<E> tail = null; private class Node<E> { E value; Node<E> next; public Node(E value) { this.value = value; } } //往尾部(即tail节点以后)添加节点 public void addNode(E value) { Node<E> newNode = new Node<>(value); if (head == null) { //head为null,即链表为空,head指向新建节点。 head = newNode; } else { tail.next = newNode; } //tail指向新节点,即新节点至关于链表的尾部 tail = newNode; //tail.next即新节点next指向head,造成环 tail.next = head; } //查找,链表中是否包含数据为searchValue的节点 public boolean containsNode(E searchValue) { Node<E> currentNode = head; if (head == null) { return false; } else { //以head开始依次向后查找,直到碰到的还是head中止。找到返回 do { if (currentNode.value == searchValue) { return true; } currentNode = currentNode.next; } while (currentNode != head); return false; } } //删除查找到的第一个数据为valueToDelete的节点 public void deleteNode(E valueToDelete) { Node<E> currentNode = head; if (head != null) { if (currentNode.value == valueToDelete) { //head节点且值为valueToDelete head = head.next; tail.next = head; } else { //以head开始依次向后查找,直到碰到的还是head中止。找到删除 do { Node<E> next = currentNode.next; if (next.value == valueToDelete) { currentNode.next = next.next; break; } currentNode = currentNode.next; } while (currentNode != head); } } }
简单作了个表格node
链表
|
数组
|
动态分配:须要时才分配内存
|
固定分配:大小固定,new时即分配全部内存
|
分散存储:内存中不连续存储
|
连续存储:内存中连续存储
|
总量限制:因为分散存储,受内存总量限制
|
使用限制:因为连续存储,若无合适连续控件即没法完成分配,且容易造成内存碎片
|
插入/删除方便:改变节点中指向next的或者prev的引用便可
|
插入/删除代价大:需建立新数组并移动元素
|
有内存浪费:节点中需额外存储next或prev的信息
|
无内存浪费:数组元素只存放数据
|
顺序访问:在某个节点沿某一方向只能逐一访问
|
随机访问:能够直接计算获得某一元素的地址
|
操做
|
链表
|
数组
|
访问(访问第N个元素)
|
O(n)
|
O(1)
|
插入到首部
|
O(1)
|
O(n)
|
插入到尾部
|
有尾部引用tail:O(1)
无尾部引用O(n)
|
O(n)
|
插入到中部
|
查找时间+O(1)~=O(n)
|
O(n) |
查找
|
O(n)
|
O(n) |
删除 |
相似插入,看删除位置或者链表的设计
|
O(n)
|