数据结构和算法------LinkedList的实现
2016-11-13 16:36
381 查看
package com.example; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; /** * LinkedList泛型类的实现 * Created by zfy on 2016/11/13. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * List长度 */ private int theSize; /** * 代表自从构造以来,对链表所做改变的次数add||remove调用的次数 */ private int modCount = 0; /** * 头结点 */ private Node<AnyType> beginMarker; /** * 未尾节点 */ private Node<AnyType> endMarker; /** * 构造方法 * * @author ZFY */ MyLinkedList() { doClear(); } /** * 清除初始化List的接口,供外部调用 * * @author ZFY */ public void clear() { doClear(); } /** * 清除初始化List的私有方法 创建并连接头结点和尾节点,然后设置大小为0 * * @author ZFY */ private void doClear() { beginMarker = new Node<>(null, null, null); endMarker = new Node<>(null, beginMarker, null); beginMarker.next = endMarker; theSize = 0; modCount++; } /** * 返回List大小的方法 * * @return List大小 * @author ZFY */ public int size() { return theSize; } /** * 判空 * * @return true:空; * <p> * false:非空; * @author ZFY */ public boolean isEmpty() { return size() == 0; } /** * 添加元素到列表尾部 * * @param x 要添加的元素. * @return true 表示添加成功. * @author ZFY */ public boolean add(AnyType x) { add(size(), x); return true; } /** * 添加元素到指定位置 * * @param index 添加元素的位置. * @param x 添加的元素. * @author ZFY */ public void add(int index, AnyType x) { addBefore(getNode(index, 0, size()), x); } /** * 查找指定位置处的节点的data域值 * * @param index * @return * @author ZFY */ public AnyType get(int index) { return getNode(index).data; } /** * @param index 位置索引 * @param newVal 新值 * @return oldVal 被替换的节点的数据域 * @author ZFY */ public AnyType set(int index, AnyType newVal) { Node<AnyType> p = getNode(index); AnyType oldVal = p.data; p.data = newVal; return oldVal; } /* * 在p指向的节点之前添加一个节点 * * */ private void addBefore(Node<AnyType> p, AnyType x) { /* 写法一: */ Node<AnyType> newNode = new Node<>(x, p.prev, p); newNode.prev.next = newNode; p.prev = newNode; /* * 写法二: Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p); * p.prev=newNode.prev.next = newNode; */ /* * 写法三: * p.prev=newNode.prev.next = new Node<AnyType>(x, p.prev, p); */ theSize++; //调整当前List的长度 modCount++;//调整对当前List的操作次数 } /* * 删除index处的节点 * */ public AnyType remove(int index) { return remove(getNode(index)); } /* * 删除p节点 * * */ private AnyType remove(Node<AnyType> p) { p.next.prev = p.prev; p.prev.next = p.next; theSize--;//调整当前List的长度 modCount++;//调整对当前List的操作次数 return p.data; } /* * 查找索引index处的节点 * */ private Node<AnyType> getNode(int index) { return getNode(index, 0, size() - 1); } /** * 查找索引index处的节点.并且范围在lower和upper之间 * * @param index index to search at. * @param lower lowest valid index. * @param upper highest valid index. * @return internal node corresponding to index * @throws IndexOutOfBoundsException if index is not between lower and upper,inclusive * @author ZFY */ private Node<AnyType> getNode(int index, int lower, int upper) { Node<AnyType> p; //如果不在之情范围内,则抛出异常 if (index < lower || index > upper) { throw new IndexOutOfBoundsException(); } // 如果索引index小于列表长度的一般,那么从表头节点开始查找 if (index < size() / 2) { p = beginMarker.next; for (int i = 0; i < index; i++) { p = p.next; } } else { // 如果索引index大于列表长度的一般,从表尾节点开始查找 p = endMarker; for (int i = size(); i > index; i--) { p = p.prev; } } return p; } @Override public Iterator<AnyType> iterator() { return new LinkedListIterator(); } /** * 描述双链表的一个节点的类 * * @author ZFY */ private static class Node<AnyType> { AnyType data; // 数据域 Node<AnyType> prev;// 前指针所指向的节点 Node<AnyType> next;// 后指针所指向的节点 Node(AnyType d, Node<AnyType> p, Node<AnyType> n) { data = d; prev = p; next = n; } } private class LinkedListIterator implements Iterator<AnyType> { //表示包由调用next所返回的项的节点 private Node<AnyType> current = beginMarker.next; //用于防止在迭代时候修改List private int expectedModCount = modCount; /*标记当前是否可以移除结合中的元素的标记量 每次调用 next 只能调用一次remove方法。 故,在next方法中置为true(表示当前可以移除元素),在remove方法中置为false(表示当前不能移除元素); */ 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; // System.out.print("current:" + current.data); return nextItem; } @Override public void remove() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } if (!okToRemove) { throw new IllegalStateException(); } MyLinkedList.this.remove(current.prev); expectedModCount++; okToRemove = false; } } }
数据结构和算法——ArrayList的实现
相关文章推荐
- Android版数据结构与算法(三):基于链表的实现LinkedList源码彻底分析
- 黑马程序员——用LinkedList实现队列和堆栈数据结构
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- 数据结构(LinkedList的java实现)
- 使用LinkedList实现栈数据结构特点的容器和队列数据结构特点的容器
- ssdb底层实现——ssdb底层是leveldb,leveldb根本上是skiplist(例如为存储多个list items,必然有多个item key,而非暴力string cat),用它来做redis的list和set等,势必在数据结构和算法层面上有诸多不适
- 数据结构与算法---C#实现LinkedList实例
- 基于LinkedList实现的固定大小线性排序数据结构
- ArrayList和LinkedList的算法实现和区别
- java 集合框架-利用LinkedList实现堆栈数据结构
- 【数据结构】LinkedList原理及实现学习总结
- 【LeetCode-面试算法经典-Java实现】【092-Reverse Linked List II(反转单链表II)】
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- [java数据结构]--java双向链表LinkedList的简单实现
- java 利用 LinkedList类实现 数据结构 栈.......
- 数据结构(Java语言)——LinkedList简单实现
- 【LeetCode-面试算法经典-Java实现】【206-Reverse Linked List(反转一个单链表)】
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- python算法:LinkedList(双向线性链表)的实现
- 【数据结构】链表LinkedList分析与关键实现