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 ...
随机推荐
- iOS iOS8新特性--UIPopoverPresentationController
1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...
- 【小TIP】记录各种错误【更新中】
最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...
- CDOJ 92 Journey(LCA&RMQ)
题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...
- State 状态模式
简介 状态模式允许一个对象在其内部[状态]改变的时候改变其[行为].这个对象看上去就像是改变了它的类一样. 用一句话来表述,状态模式把所研究的对象的[行为]包装在不同的[状态对象]里,[每一个状态对象 ...
- js页面加载事件
<body onload="myfunction()" > </body> <script type="text/javascript&qu ...
- 【开源java游戏框架libgdx专题】-12-开发工具-图片合成
TexturePackerGui工具: 1.工具使用: 首先看到texturepacker的界面 界面介绍: New pack:创建项目按钮,单击后输入文件名称,创建文件. Input directo ...
- springmvc学习笔记(理论)
1.springmvc是什么? Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层 进行职责解耦,基 ...
- java计算两个日期之间相隔的天数
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- oracle事务特性详解
原子性 事务是一个完整的操作.事务的各步操作是不可分的(原子的):要么都执行,要么都不执行. -- 创建表 create table account_money ( id number(4) not ...
- 四大跨平台的APP分析
转载:http://blog.csdn.net/kenkao/article/details/50678269