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;
else
pred.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 situation
if (l == null)
first = newNode;
else
l.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);
else
linkBefore(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;
else
pred.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 ...
随机推荐
- Camera2Raw
This sample demonstrates how to use the Camera2 API to capture RAW camera buffers and save them as D ...
- 浪漫桃心的Android表白程序
本文转载于 huachao1001的专栏 几年前,看到过有个牛人用HTML5绘制了浪漫的爱心表白动画.地址在这:浪漫程序员 HTML5爱心表白动画.发现原来程序员也是可以很浪……漫…..的.那么在A ...
- 关于php读mysql数据库时出现乱码的解决方法
关于php读mysql数据库时出现乱码的解决方法 php读mysql时,有以下几个地方涉及到了字符集. 1.建立数据库表时指定数据库表的字符集.例如 create table tablename ( ...
- Android之使用json进行网络数据交换
JSON作为一种轻量级的数据交换格式,凭借其易于阅读和编写.易于解析.传输速度快等优点流行了起来.最近正好在学习Android端从服务端端取数据,Json便派上了用场.好,下面开始切入主题. 1.准备 ...
- javadoc in archive eclipse.
Open Windows -> Preferences. 2.Select jre from Installed JREs then hit Edit botton on the right. ...
- asp.net 读取sql存储过程返回值
关于Exec返回值的问题有很多,在这做个简要的总结. 读查询语句示例: Declare @count int select @Count 要点: ...
- WebSocket协议
websocket 简介 (2013-04-09 15:39:28) 转载▼ 分类: websocket 一 WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例 ...
- 高性能IO设计模式之阻塞/非阻塞,同步/异步解析
提到高性能,我想大家都喜欢这个,今天我们就主要来弄明白在高性能的I/O设计中的几个关键概念,做任何事最重要的第一步就是要把概念弄的清晰无误不是么?在这里就是:阻塞,非阻塞,同步,异步. OK, 现在来 ...
- 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson
根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...
- 【USACO 2.2.3】循环数
[题目描述] 循环数是那些不包括0且没有重复数字的整数(比如81362)并且还应同时具有一个有趣的性质, 就像这个例子: 如果你从最左边的数字开始(在这个例子中是8)向右数最左边这个数(如果数到了最右 ...