LinkedList类的实现
来源:互联网 发布:全球酒店排名 知乎 编辑:程序博客网 时间:2024/06/01 20:53
本次简单介绍LinkedList泛型类的实现,为避免与类库中的类相混,将类名定义为MyLinkedList。下面概括需要实现的细节。
MyLinked类本身,它包含到两端的链、表的大小以及一些方法。
Node类,它可能是一个私有的嵌套类。一个节点包含数据以及到前一个节点的链和到后一个节点的链,还有一些适当的构造方法。
MyLinkedList将提供一个实现Iterator接口的类。这个类将存储迭代序列中的下一项的下标,并提供next、hasNext和remove等方法的实现。MyArrayList的迭代器方法直接返回实现的Iterator接口的该类的新构造的实例。
package com.zs.structures.list.linkedlist;import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.NoSuchElementException;/** * LinkedList类实现 * @author ZS * */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 Node(AnyType data, Node<AnyType> prev, Node<AnyType> next) { this.data = data; this.prev = prev; this.next = next; } private AnyType data; private Node<AnyType> prev; private Node<AnyType> next; } 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 theSize == 0; } public boolean add(AnyType x) { add(theSize, x); return true; } public boolean add(int index, AnyType x) { addBefore(getNode(index), x); return true; } public AnyType get(int index) { return getNode(index).data; } public AnyType set(int index, AnyType x) { Node<AnyType> old = getNode(index); AnyType oldVal = old.data; old.data = x; return oldVal; } public AnyType remove(int index) { return remove(getNode(index)); } 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 index) { if(index < 0 || index > theSize) { throw new ArrayIndexOutOfBoundsException(); } Node<AnyType> node; if(index < theSize / 2) { node = beginMarker.next; for (int i = 0; i < index; i++) { node = node.next; } } else { node = endMarker; for (int i = theSize; i > index; i--) { node = node.prev; } } return node; } @Override public Iterator<AnyType> iterator() { return new LinkedListIterator(); } private class LinkedListIterator implements Iterator<AnyType> { private int expectedModCount = modCount; private Node<AnyType> current = beginMarker.next; 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 0
- LinkedList类的实现
- LinkedList类的实现
- java中链表的实现类LinkedList
- List接口的实现类LinkedList
- LinkedLIst的实现原理
- LinkedList的实现原理
- 对linkedlist的实现
- java LinkedList 的实现
- LinkedList的实现
- 实现自己的LinkedList
- LinkedList的实现原理
- LinkedList的手动实现
- 自己实现的LinkedList
- LinkedList的实现原理
- 类实现之LinkedList
- LinkedList实现类
- 详解LinkedList实现类
- 数据结构(LinkedList的java实现)
- 下沉的船
- pandas基本命令操作
- 2017智慧树 伟大的红楼梦测试答案题库
- 字符串反转
- HNOI2017-伤感之中充满希望
- LinkedList类的实现
- 进程间的通信之命名管道(FIFO)
- path模块
- 输入三个坐标,判断能否构成三角形,如果能,输出其周长和面积。
- BZOJ3224[Tyvj 1728 普通平衡树]题解--Treap
- 分类游戏
- Ball HDU
- 比特币基于区块链开发的一种技术,这种数值货币本身有什么价值
- 动态的页面包含指令和静态的页面包含指令之间的区别