Foundation Data Structure
LinkedList :
/*** 考虑的比较的周全,并且包含了全部的情况,代码也不乱<b></b>** @param index* 插入的位置* @param c* 插入的元素集合*/private boolean addAll(int index, Collection<? extends E> c) {// 缺少对于size的参数的校验Object[] a = c.toArray();int numNew = a.length;if (numNew == 0)return false;// 确定插入的位置和插入的前一个位置Node<E> pred, succ;if (index == size) {succ = null;pred = last;} else {succ = node(index);pred = succ.prev;}// 首先就是能够确认自己前面的一个节点是谁,size指的是插入的位置,后面的全部的一次// 向后移动for (Object o : a) {@SuppressWarnings("unchecked")E e = (E) o;Node<E> newNode = new Node<E>(pred, e, null);if (pred == null)first = newNode;elsepred.next = newNode;pred = newNode;}// 把插入后面的元素接上if (succ == null) {last = pred;} else {pred.next = succ;succ.prev = pred;}size += numNew;return true;}/*** Returns the (non-null) Node at the specified element index. the alg is* interesting*/private Node<E> node(int index) {if (index < (size >> 1)) {// 前半截,后半截Node<E> x = first;for (int i = 0; i < index; i++)x = x.next;return x;} else {Node<E> x = last;for (int i = size - 1; i > index; i--)x = x.prev;return x;}}
public boolean add(E e) {linkLast(e);return true;}/*** @param e*/private void linkLast(E e) {// 尾节点,final ?final Node<E> l = last;final Node<E> newNode = new Node<E>(l, e, null);last = newNode;// special situationif (l == null)first = newNode;elsel.next = newNode;size++;}/*** Inserts the specified element at the specified position in this list.* Shifts the element currently at that position (if any) and any subsequent* elements to the right (adds one to their indices).** @param index* index at which the specified element is to be inserted* @param element* element to be inserted* @throws IndexOutOfBoundsException* {@inheritDoc}*/public void add(int index, E element) {checkPositionIndex(index);if (index == size)linkLast(element);elselinkBefore(element, node(index));}private void linkBefore(E element, Node<E> succ) {// assert succ != null;final Node<E> pred = succ.prev;final Node<E> insert = new Node<E>(pred, element, succ);succ.prev = insert;if (pred == null)first = insert;elsepred.next = insert;size++;}private boolean isPositionIndex(int index) {return index >= 0 && index <= size;}private void checkPositionIndex(int index) {if (!isPositionIndex(index))throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private String outOfBoundsMsg(int index) {return "Index: " + index + ", Size: " + size;}
Foundation Data Structure的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
随机推荐
- 在 Java 中高效使用锁的技巧--转载
竞争锁是造成多线程应用程序性能瓶颈的主要原因 区分竞争锁和非竞争锁对性能的影响非常重要.如果一个锁自始至终只被一个线程使用,那么 JVM 有能力优化它带来的绝大部分损耗.如果一个锁被多个线程使用过,但 ...
- 模拟电路"虚短" & "虚断"
<虚短 & 虚断> 运算放大器组成的电路五花八门,令人眼花瞭乱,是模拟电路中学习的重点.遍观所有模拟电子技朮的书籍和课程,在介绍运算放大器电路的时候,无非是先给电路来个定性,比如这 ...
- Command 命令模式
简介 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于[行为请求者]与[行为实现者]解耦,可实现二者之间的松耦合,以便适应变化. 将一个请求封装为一个对象,从而使你可用不同 ...
- bzoj 1034 (田忌赛马++)
/* 这类题的最优策略: 自己最好的干掉对方最好的 或者 自己最差的干掉对方最差的 不能的话 用自己最差的 对阵对方最好的 这样是最优的 实现嘛 搞两个队列跑一跑 */ #include<ios ...
- mysql 优化点小结
1.数据库表设计的合理性 1)三范式 一范式:原子性,属性不可分: 二范式:无部分依赖, 例:(学号, 课程名称) → (姓名, 年龄, 成绩, 学分),存在部分依赖 (学号) → (姓名, 年龄) ...
- ASP.NET生成压缩文件(rar打包)
首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZi ...
- iOS 中Window优先级的问题
在项目中,视频播放时候遇到网络切换需要弹出AlertView提醒用户,忽然发现转屏的时候播放View加到KeyWindow的时候把AleryView挡住了.如图 因为转屏的时候视图是直接加载到 [UI ...
- cas sso单点登录系列4_cas-server登录页面自定义修改过程(jsp页面修改)
转:http://blog.csdn.net/ae6623/article/details/8861065 SSO单点登录系列4:cas-server登录页面自定义修改过程,全新DIY. 目标: ...
- Linux服务器指令
1.查看cpu信息:/proc/cpuinfo2.查看内存信息:/prco/meminfo3.查看服务器版本信息:cat /etc/issue4.服务器系统位数:uname -a5.网卡信息:ifco ...
- 03_RHEL7.1去掉注册提示
# rpm –qa|grep subscription-manager 出现类似下面的代码: subscription-manager-firstboot-1.13.22-1.el7.x86_64 s ...