简单实现MyLinkedList
2018-01-05 18:42
405 查看
package cn.limbo.java_structure; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; /** * 1:首先包含到两端的链,表的大小以及其他方法 * 2:Node类,可以设计为一个私有的嵌套类(内部类).一个节点包含数据以及 * 到前一个节点的链和到下一个节点的链,必须要有构造方法 * 3:LinkedListIterator类,该类抽象了位置的概念,是一个私有类,并且实现了 * 接口Iterator,并且提供了next hasNext remove方法. * 主要解决前后是否有节点的问题 * 1:区别节点和当前元素 * 2:链表结构基本操作都是对引用的操作 */ public class MyLinkedList<T> implements Iterable<T> { private int theSize;//表示当前数组的长度 private int modCount = 0; private Node<T> beginMarker; private Node<T> endMarker; public MyLinkedList() { clear(); } //不同于MyArrayList /** * change the size if this collection to zero */ private void clear() { beginMarker = new Node<>(null, null, null); endMarker = new Node<>(null, beginMarker, null); beginMarker.next = endMarker; theSize = 0; modCount++; } public int size() { return theSize; } public boolean isEmpty() { return size() == 0; } public boolean add(T t) { add(size(), t); return true; } private void add(int idx, T t) { addBefore(getNode(idx), t); } public T get(int idx) { return getNode(idx).data; } public T set(int idx, T newVal) { Node<T> p = getNode(idx); T oldVal = p.data; p.data = newVal; return oldVal; } public T remove(int idx) { return remove(getNode(idx)); } private void addBefore(Node<T> p, T x) { //向一个双链表中的插入操作到p点的位置 Node<T> newNode = new Node<T>(x, p.prev, p); newNode.prev.next = newNode; p.prev = newNode; theSize++; modCount++; } /** * 从一个双链表中删除由p指定的节点 */ private T remove(Node<T> p) { p.next.prev = p.prev; p.prev.next = p.next; theSize--; modCount--; return p.data; } /** * Get the Node at position idx,which must range from 0 to size(). */ private Node<T> getNode(int idx) { Node<T> 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; } @Override public Iterator<T> iterator() { return new LinkedListIterator(); } //私有节点类 private static class Node<T> { public T data; public Node<T> prev; public Node<T> next; public Node(T t, Node<T> p, Node<T> n) { data = t; prev = p; next = n; } } private class LinkedListIterator implements Iterator<T> { private Node<T> current = beginMarker.next; private int expectedModCount = modCount; private boolean okToRemove = false; public boolean hasNext() { return current != endMarker; } public T next() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } if (!hasNext()) { throw new NoSuchElementException(); } T 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++; } } }
相关文章推荐
- LinkedList以及Iterator的简单实现
- 数据结构(Java语言)——LinkedList简单实现
- 数据结构: Java中LinkedList的简单实现
- ArrayList和LinkedList的简单实现
- Java集合框架之如何用LinkedList实现简单的栈和队列
- MyLinkedList(链表的实现)
- 简单ArrayList、LinkedList、HashSet、HashMap实现(一)
- 自己动手系列——实现一个简单的LinkedList
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- LinkedList简单实现
- [java数据结构]--java双向链表LinkedList的简单实现
- linkedlist简单实现。
- LinkedList双链表简单分析,及单链表的实现代码
- Java 集合的简单实现 (ArrayList & LinkedList & Queue & Stack)
- LinkedList的简单实现(手写)
- Python数据结构与算法之列表(链表,linked list)简单实现
- 数据结构之LinkedList的简单实现
- LinkedList链表实现队列应用的简单实例
- 如何实现Java中一个简单的LinkedList
- LinkedList 实现栈与队列,比较简单