深度剖析Java数据结构之表(四)——LinkedList泛型类的实现

在Java中,LinkedList泛型类继承了AbstractSequentialListf泛型类,实现了List、Deque、Colneable和Serializable接口,定义如下:

该类的实现使用的是循环链表的结构,我们知道,一个链表的主要标志就是表头,所以一个LinkedList泛型类中一定要有一个头结点,链表的组成元素是结点,所以还需要在该类中定义一个结点的内部类,当然,也可以在外面定义,为了实现更好的封装性,定义成private的内部类是再好不过的了。

下面是定义的头结点和内部结点类:

private transient Entry<E> header = new Entry<E>(null, null, null); private transient int size = 0;

在定义header的时候,使用了一个transient修饰符,在Java中,被该修饰符修饰的变量在串行化的时候(序列化)的时候,不会序列化该变量。因为一个链表,如果需要串行化,我们需要的信息只是结点中的信息,所以头结点可以定义成一个被transient修饰的变量。同时还定义了一个size变量,用来保存链表的长度,同样,它也应该使用transient修饰符。

下面是定义的结点类Entry类:

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


一个结点除了要保存数据之外,还应该保存它的后继(单项链表),由于该类提供一些操作,需要向前遍历,所以还需要有一个保存前驱的信息,即privious引用。

下面是LinkedList泛型类中的listIterator()方法:

public ListIterator<E> listIterator() { return listIterator(0); } public ListIterator<E> listIterator(int index) { return new ListItr(index); } private class ListItr implements ListIterator<E> { private Entry<E> lastReturned = header; private Entry<E> next; private int nextIndex; private int expectedModCount = modCount; ListItr(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); if (index < (size >> 1)) { next = header.next; for (nextIndex=0; nextIndex<index; nextIndex++) next = next.next; } else { next = header; for (nextIndex=size; nextIndex>index; nextIndex--) next = next.previous; } } public boolean hasNext() { return nextIndex != size; } public E next() { checkForComodification(); if (nextIndex == size) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.element; } public boolean hasPrevious() { return nextIndex != 0; } public E previous() { if (nextIndex == 0) throw new NoSuchElementException(); lastReturned = next = next.previous; nextIndex--; checkForComodification(); return lastReturned.element; } public int nextIndex() { return nextIndex; } public int previousIndex() { return nextIndex-1; } public void remove() { checkForComodification(); Entry<E> lastNext = lastReturned.next; try { LinkedList.this.remove(lastReturned); } catch (NoSuchElementException e) { throw new IllegalStateException(); } if (next==lastReturned) next = lastNext; else nextIndex--; lastReturned = header; expectedModCount++; } public void set(E e) { if (lastReturned == header) throw new IllegalStateException(); checkForComodification(); lastReturned.element = e; } public void add(E e) { checkForComodification(); lastReturned = header; addBefore(e, next); nextIndex++; expectedModCount++; } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }

还有一些操作,主要是对引用的修改。在此不一一列出了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值