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 ...
随机推荐
- [Android] Service和IntentService中显示Toast的区别
1. 表象 Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失. 2. ...
- HDFS Architecture--官方文档
HDFS Architecture Introduction The Hadoop Distributed File System (HDFS) is a distributed file syste ...
- MVC模式下的数据展示:EasyUI的datagrid
我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...
- discuz! X3 门户文章添加字段
1. 首先需要去数据表里[llgp_portal_article_title]手动添加需要添加的字段. (注意: 数据表前缀依据自己的设置而定) 2. 修改模版template\default\por ...
- 如何在ASP.NET端获取屏幕宽度
using System.Windows.Forms; 首先引用上面的命名空间. 然后在代码中获取屏幕信息.如下代码: System.Windows.Forms.Screen screen = Sys ...
- DWZ框架学习一
测试DWZ框架弹出框设置成模态 刚刚上手DWZ框架,感觉灰常好用,对于我这种特别懒的人来说,真的是拖拽编程 看了下官方的视频讲解,自己试着做了一个小测试,里面的组件什么的都不用写,直接拿来用 这里附上 ...
- My First Blog.
I just wanna mark my first blog in order to have a wonderful memories in the future.
- Android中使用PullToRefreshListView遇到的问题
1.布局文件中要是设置visible属性为Gone的时候,注意了,这样会有一个bug,在代码中setVisible的时候设置为VISIBLE是不起作用的..这个应该是自身的一个小bug(本人目前没有找 ...
- jrae源码解析(二)
本文细述上文引出的RAECost和SoftmaxCost两个类. SoftmaxCost 我们已经知道,SoftmaxCost类在给定features和label的情况下(超参数给定),衡量给定权重( ...
- [转] UIImage 图像-IOS开发 (实例)
转自 http://justcoding.iteye.com/blog/1470931 一 UIImageView 简介 UIImageView是显示图片的控件,显示图片时,首先需要把图片加载到UI ...