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 ...
随机推荐
- Miller_Rabin codevs 1702 素数判定2
/* 直接费马小定理 */ #include<iostream> #include<cstdio> #include<cstdlib> #include<ct ...
- css3中-webkit-text-size-adjust详解
1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text-size-adjust:none;} 2.-webk ...
- jdbc_odbc SQLserver 驱动安装及测试
有2次被问到同一个问题,尽管博客园是.net的园子,我还是分享下吧.PS:我现在做的.net,以前学过点java.献丑了. ------------------ 原始邮件 -------------- ...
- Linux sed命令删除指定行
一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...
- 使用<pre>标签为你的网页加入大段代码
在上节中介绍加入一行代码的标签为<code>,但是在大多数情况下是需要加入大段代码的,如下图: 怎么办?不会是每一代码都加入一个<code>标签吧,没有这么复杂,这时候就可以使 ...
- Swift - 24 - switch语句的高级用法
//: Playground - noun: a place where people can play import UIKit // 对区间进行判断 var score = 90 switch s ...
- json+一般处理程序读取数据库数据
一般处理程序的语法结构 string jsoncallback = context.Request["jsoncallback"]; 声明变量 前台传值使用 stri ...
- Starting and Stopping Oracle Fusion Middleware
指定用户名密码启动管理服务器 You can start and stop Oracle WebLogic Server Administration Servers using the WLST c ...
- Spring依赖注入的三种方式
看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...
- JQUERY1.9学习笔记 之基本过滤器(十二) 根元素选择器
根元素选择器 描述:选择文档的根节点元素.jQuery( ":root" ) 例:显示文档根节点标签名. <!DOCTYPE html><html lang=&q ...