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 ...
随机推荐
- Swift 算法实战之路:基本语法与技巧
Swift是苹果新推出的编程语言,也是苹果首个开源语言.相比于原来的Objective-C,Swift要更轻便和灵活.笔者最近使用Swift实践了大量的算法(绝大部分是硅谷各大公司的面试题),将心得体 ...
- 给iOS开发者的GCD用户手册
Grand Central Dispatch,或者GCD,是一个极其强大的工具.它给你一些底层的组件,像队列和信号量,让你可以通过一些有趣的方式来获得有用的多线程效果.可惜的是,这个基于C的API是一 ...
- 使用ssh对服务器进行登录
一.什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会 ...
- ZOJ1463
题意:给一个括号字符串,求解最少添加的字符能使整个字符串匹配. 输入: s(未匹配的字符串) 输出: S(匹配后的字符串) 思路:绝壁超级坑的一道题,格式我不想说什么了,特坑,然后就是对给定的字符串, ...
- 移动端屏幕自适应js与rem
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;&qu ...
- js的new操作符
1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . ...
- PHP微信公众号 access_token缓存
PHP创建access_token.json文件,将access_token 和 生成时间expires 保存在其中, {"access_token":"xxxx&quo ...
- Python os常用模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...
- firefox 的event事件处理
前几天,在用angularJs实现一个功能,点击后获取event的x,y坐标时,IE9, chrome下功能正常.但是firefox报event 未定义.初始代码如下: html: <div c ...
- 重新开始学习javase_一切都是对象
@学习thinking in java 一,一切都是对象 用句柄操纵对象 每种编程语言都有自己的数据处理方式.比如说c与c++中的指针,而java中尽管将一切都“看作”对象,但操纵的标识符实际是指向一 ...