LinkedList详解

下面我们来详细讲解LinkedList的详细信息,希望能对您有所帮助。
LinkedList的特点如下:
1。LinkedList属于双向链表(环状)。
2。没有实现线程安全。
3。此类实现 Queue 接口,提供先进先出队列操作。
4。此类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列 (deque)。

下面来重点讲解LinkedList的内部结构。
1。链表的实现类。
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;
	}
    }


此类就是LinkedList的用于实现链表的类。
该类中
第一个参数记录了对象的值。
第二个参数用于记录下一个参数的实体类
第三个参数用于记录上一个参数的实体类

2。构造函数
LinkedList提供两种构造函数一种是无参构造函数,集合类的参数够着函数。
//此处用于构建空的LinkedList
public LinkedList() {
        header.next = header.previous = header;
    }

//有参数构造函数
public LinkedList(Collection<? extends E> c) {
	 this();
	 addAll(c);
     }


重点讲解:
无参数构造函数:
初始化类的时候会创建如下临时变量。
 
private transient Entry<E> header = new Entry<E>(null, null, null);

由此可以看出无参数构造函数初始化时,
创建了如下变量,并把指向上一个或者下一个指针指向自己
有参数构造函数:
1。调用无参数构造函数
2。把集合类添加到链表中来。
具体怎么添加的请查看如下代码
    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;
	modCount++;

        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;
    }

3。getFirst
public E getFirst() {
	if (size==0)
	    throw new NoSuchElementException();

	return header.next.element;
    }
4。getLast
public E getLast()  {
	if (size==0)
	    throw new NoSuchElementException();

	return header.previous.element;
    }
5。删除元素主要方法remove(Entry<E> e)
 private E remove(Entry<E> e) {
	if (e == header)
	    throw new NoSuchElementException();

        E result = e.element;
	e.previous.next = e.next;
	e.next.previous = e.previous;
        e.next = e.previous = null;
        e.element = null;
	size--;
	modCount++;
        return result;
    }
6。添加元素主要方法addBefore(o, header.next);
private Entry<E> addBefore(E o, Entry<E> e) {
	Entry<E> newEntry = new Entry<E>(o, e, e.previous);
	newEntry.previous.next = newEntry;
	newEntry.next.previous = newEntry;
	size++;
	modCount++;
	return newEntry;
    }

7。contains(Object o)方法中最主要的方法如下:
public int indexOf(Object o) {
        int index = 0;
        if (o==null) {
            for (Entry e = header.next; e != header; e = e.next) {
                if (e.element==null)
                    return index;
                index++;
            }
        } else {
            for (Entry e = header.next; e != header; e = e.next) {
                if (o.equals(e.element))
                    return index;
                index++;
            }
        }
        return -1;
    }


8。addAll(Collection<? extends E> c)主要方法
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;
	modCount++;

        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;
    }

 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;
    }


9。 clear() 清除
ublic void clear() {
        Entry<E> e = header.next;
        while (e != header) {
            Entry<E> next = e.next;
            e.next = e.previous = null;
            e.element = null;
            e = next;
        }
        header.next = header.previous = header;
        size = 0;
	modCount++;
    }


10。set(int index, E element)
public E set(int index, E element) {
        Entry<E> e = entry(index);
        E oldVal = e.element;
        e.element = element;
        return oldVal;
    }
11。public Object clone()
 public Object clone() {
        LinkedList<E> clone = null;
	try {
	    clone = (LinkedList<E>) super.clone();
	} catch (CloneNotSupportedException e) {
	    throw new InternalError();
	}

        // Put clone into "virgin" state
        clone.header = new Entry<E>(null, null, null);
        clone.header.next = clone.header.previous = clone.header;
        clone.size = 0;
        clone.modCount = 0;

        // Initialize clone with our elements
        for (Entry<E> e = header.next; e != header; e = e.next)
            clone.add(e.element);

        return clone;
    }

 public Object[] toArray() {
	Object[] result = new Object[size];
        int i = 0;
        for (Entry<E> e = header.next; e != header; e = e.next)
            result[i++] = e.element;
	return result;
    }





















你可能感兴趣的:(C++,c,C#)