下面是一个能够使用的LinkedList泛型类的实现。这里的链表类名为MyLinkedList,避免与类库中反复。

MyLinkedList将作为双链表实现,并且保留到该表两端的引用。这样仅仅要操作发生在已知的位置,就能够保持每一个操作花费常数时间的代价。这个已知的位置能够是端点。也能够是由迭代器指定的一个位置。

设计方面,主要分为三个部分实现:

  1. MyLinkedList类本身,包括到两端的链、表的大小以及一些方法。
  2. Node类,它可能是一个私有的嵌套类。一个节点包括数据以及到前一个节点的链和到下一个节点的链,另一些适当的构造方法。
  3. LinkedListIterator类,该类抽象了位置的概念,是一个私有类,并实现接口Iterator。

    包括方法next(),hasNext(),remove()的实现。

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException; public class MyLinkedList<AnyType> implements Iterable<AnyType> {
private int theSize;
private int modCount = 0;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker; private static class Node<AnyType> {
public AnyType data;
public Node<AnyType> prev;
public Node<AnyType> next; public Node(AnyType d, Node<AnyType> p, Node<AnyType> n) {
data = d;
prev = p;
next = n;
}
} public MyLinkedList() {
clear();
} public void clear() {
beginMarker = new Node<AnyType>(null, null, null);
endMarker = new Node<AnyType>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
} public int size() {
return theSize;
} public boolean isEmpty() {
return size() == 0;
} public void add(AnyType x) {
add(size(), x);
} public void add(int idx, AnyType x) {
addBefore(getNode(idx), x);
} public AnyType get(int idx) {
return getNode(idx).data;
} public AnyType set(int idx, AnyType newVal) {
Node<AnyType> p = getNode(idx);
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
} public AnyType reomve(int idx) {
return remove(getNode(idx));
} private void addBefore(Node<AnyType> p, AnyType x) {
Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
} private AnyType remove(Node<AnyType> p) {
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
} private Node<AnyType> getNode(int idx) {
Node<AnyType> p;
if (idx < 0 || idx > size()) {
throw new IndexOutOfBoundsException();
}
if (idx < size() / 2) {
p = beginMarker.next;
for (int i = 0; i < idx; i++) {
p = p.next;
}
} else {
p = endMarker;
for (int i = size(); i < idx; i--) {
p = p.prev;
}
}
return p;
} public Iterator<AnyType> iterator() {
return new LinkedListIterator();
} private class LinkedListIterator implements Iterator<AnyType> {
private Node<AnyType> current = beginMarker.next;
private int expectedModCount = modCount;
private boolean okToRemove = false; @Override
public boolean hasNext() {
return current != endMarker;
} @Override
public AnyType next() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}
AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
} public void remove() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!okToRemove) {
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
okToRemove = false;
expectedModCount++;
}
}
}

最新文章

  1. TCP三次握手建立连接
  2. 关于Azure带宽的测试
  3. mybatis使用generator生成对应的model、mapping配置文件、dao
  4. iscsi与multipath
  5. Maven 跳过测试目录
  6. PageRank与TrustRank影响因素分析
  7. 【HDOJ】【3853】LOOPS
  8. scope引起的问题
  9. git执行cherry-pick时修改提交信息
  10. (6).NET CORE微服务 Micro-Service ---- AOP框架
  11. Mysq中的流程控制语句的用法
  12. Benelux Algorithm Programming Contest 2017(D)
  13. webstorm 格式化代码及常用快捷键
  14. Java知多少(20)变量的作用域
  15. LeetCode OJ 94. Binary Tree Inorder Traversal
  16. Flask初级(九)flash与前台交互get详解
  17. 任务三 简单程序测试及 GitHub Issues 的使用
  18. Rest Framework源码流程解析
  19. Docker Compose 多容器应用
  20. Jenkins+Gitlab+Ansible自动化部署(五)

热门文章

  1. SQL Server的自动备份设置及排错记事
  2. Python的filter与map内置函数
  3. QlikSense移动端使用攻略
  4. C-C语言概述
  5. winFrom线程
  6. jQuery操作样式知识总结
  7. 优动漫PAINT(clip studio paint)怎么画一幅水墨竹子图
  8. vc++图像保存,重绘
  9. 天使轮 A轮 B轮 上市...等名词解释
  10. 【JavaScript框架封装】使用Prototype给Array,String,Function对象的方法扩充