package com.datastructure.List; import java.util.Collection; import java.util.NoSuchElementException; /** * 当前类为双向循环链表 该链表各元素类型可以不相同,不可反序列化 * * @author LEIQIAO * * @param <E> */ public class LinkedList<E> { private transient Entry<E> header = new Entry<E>(null, null, null); private transient int size = 0; public static void main(String[] args) { LinkedList<Integer> link = new LinkedList<Integer>(); // 测试getFirst(), removeFirst(), addFirst(); // System.out.println(link.getFirst()); link.addFirst(1); link.addFirst(2); System.out.println(link.getFirst()); link.listAll(); } /** * Constructs an empty list. */ public LinkedList() { header.next = header.previous = header; } /** * 放回当前链表的第一元素 * * @return 链表的第一个元素 * @throws NoSuchElementException * 如果当前链表为空 */ public E getFirst() { if (size == 0) throw new NoSuchElementException(); return header.next.element; } /** * 返回当前链表的最后一个元素 * * @return 最后一个元素 * @throws NoSuchElementException * 如果当前链表为空 */ public E getLast() { if (size == 0) throw new NoSuchElementException(); return header.previous.element; } /** * 移除同时返回当前链表的第一个元素 * * @return 链表的第一个元素 * @throws NoSuchElementException * 链表为空 */ public E removeFirst() { return remove(header.next); } /** * 移除且返回当前链表的最后一个元素 * * @return the last element from this list * @throws NoSuchElementException * if this list is empty */ public E removeLast() { return remove(header.previous); } /** * 添加当前链表的首元素 * * @param 元素的数据 */ public void addFirst(E e) { addBefore(e, header.next); } /** * 添加最后一个元素 * * @param 元素的数据 */ public void addLast(E e) { addBefore(e, header); } /** * 返回链表的长度 * * @return 链表元素的数量 */ public int size() { return size; } /** * 移除该链表的第一个该元素 链表元素允许为空,空则允许删除 * * @param 链表中需要移除的元素 * @return 如果该链表中包含元素则返回true */ public boolean remove(Object o) { if (o == null) { for (Entry<E> e = header.next; e != header; e = e.next) { if (e.element == null) { remove(e); return true; } } } else { for (Entry<E> e = header.next; e != header; e = e.next) { if (o.equals(e.element)) { remove(e); return true; } } } return false; } /** * 从特定位置把该集合所有元素插入到链表中. * * @param index 讲集合中第一个元素插入链表的位置 * @param c 包含插入链表元素的集合 * @return 链表改变返回真 * @throws IndexOutOfBoundsException * {@inheritDoc} * @throws NullPointerException * if the specified collection is null */ public boolean addAll(int index, Collection<? extends E> c) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Entry<E> successor = (index == size ? header : entry(index)); Entry<E> predecessor = successor.previous; for (int i = 0; i < numNew; i++) { Entry<E> e = new Entry<E>((E) a[i], successor, predecessor); predecessor.next = e; predecessor = e; } successor.previous = predecessor; size += numNew; return true; } /** * 返回索引位置的Entry<E> 索引位置从0开始,size表示元素的个数,即第size个为头结点 */ private Entry<E> entry(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); Entry<E> e = header; // 双向循环链表:前半部分顺时针查找,后半部分逆时针查找 if (index < (size >> 1)) { for (int i = 0; i <= index; i++) e = e.next; } else { for (int i = size; i > index; i--) e = e.previous; } return e; } private E remove(Entry<E> e) { // 此处不可用size判断,因为头结点为算法的需要,规范操作,不可删除 if (e == header) throw new NoSuchElementException(); E result = e.element; e.previous.next = e.next; e.next.previous = e.previous; size--; return result; } private Entry<E> addBefore(E e, Entry<E> entry) { Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); // newEntry.prrvious = entry.previous entry.previous.next = newEntry; // newEntry.next = entry entry.previous = newEntry; size++; return newEntry; } public void listAll() { for (Entry<E> p = header.next; p != header; p = p.next) { System.out.println(p.element); } } private static class Entry<E> { E element; Entry<E> next; Entry<E> previous; Entry(E element, Entry<E> next, Entry<E> previous) { this.element = element; this.next = next; this.previous = previous; } } }