Foundation Data Structure

LinkedList :

  1. /**
  2. * 考虑的比较的周全,并且包含了全部的情况,代码也不乱<b></b>
  3. *
  4. * @param index
  5. * 插入的位置
  6. * @param c
  7. * 插入的元素集合
  8. */
  9. private boolean addAll(int index, Collection<? extends E> c) {
  10. // 缺少对于size的参数的校验
  11. Object[] a = c.toArray();
  12. int numNew = a.length;
  13. if (numNew == 0)
  14. return false;
  15. // 确定插入的位置和插入的前一个位置
  16. Node<E> pred, succ;
  17. if (index == size) {
  18. succ = null;
  19. pred = last;
  20. } else {
  21. succ = node(index);
  22. pred = succ.prev;
  23. }
  24. // 首先就是能够确认自己前面的一个节点是谁,size指的是插入的位置,后面的全部的一次
  25. // 向后移动
  26. for (Object o : a) {
  27. @SuppressWarnings("unchecked")
  28. E e = (E) o;
  29. Node<E> newNode = new Node<E>(pred, e, null);
  30. if (pred == null)
  31. first = newNode;
  32. else
  33. pred.next = newNode;
  34. pred = newNode;
  35. }
  36. // 把插入后面的元素接上
  37. if (succ == null) {
  38. last = pred;
  39. } else {
  40. pred.next = succ;
  41. succ.prev = pred;
  42. }
  43. size += numNew;
  44. return true;
  45. }
  46. /**
  47. * Returns the (non-null) Node at the specified element index. the alg is
  48. * interesting
  49. */
  50. private Node<E> node(int index) {
  51. if (index < (size >> 1)) {// 前半截,后半截
  52. Node<E> x = first;
  53. for (int i = 0; i < index; i++)
  54. x = x.next;
  55. return x;
  56. } else {
  57. Node<E> x = last;
  58. for (int i = size - 1; i > index; i--)
  59. x = x.prev;
  60. return x;
  61. }
  62. }

add function 代码写的还是比较的精彩的:

  1. public boolean add(E e) {
  2. linkLast(e);
  3. return true;
  4. }
  5. /**
  6. * @param e
  7. */
  8. private void linkLast(E e) {
  9. // 尾节点,final ?
  10. final Node<E> l = last;
  11. final Node<E> newNode = new Node<E>(l, e, null);
  12. last = newNode;
  13. // special situation
  14. if (l == null)
  15. first = newNode;
  16. else
  17. l.next = newNode;
  18. size++;
  19. }
  20. /**
  21. * Inserts the specified element at the specified position in this list.
  22. * Shifts the element currently at that position (if any) and any subsequent
  23. * elements to the right (adds one to their indices).
  24. *
  25. * @param index
  26. * index at which the specified element is to be inserted
  27. * @param element
  28. * element to be inserted
  29. * @throws IndexOutOfBoundsException
  30. * {@inheritDoc}
  31. */
  32. public void add(int index, E element) {
  33. checkPositionIndex(index);
  34. if (index == size)
  35. linkLast(element);
  36. else
  37. linkBefore(element, node(index));
  38. }
  39. private void linkBefore(E element, Node<E> succ) {
  40. // assert succ != null;
  41. final Node<E> pred = succ.prev;
  42. final Node<E> insert = new Node<E>(pred, element, succ);
  43. succ.prev = insert;
  44. if (pred == null)
  45. first = insert;
  46. else
  47. pred.next = insert;
  48. size++;
  49. }
  50. private boolean isPositionIndex(int index) {
  51. return index >= 0 && index <= size;
  52. }
  53. private void checkPositionIndex(int index) {
  54. if (!isPositionIndex(index))
  55. throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  56. }
  57. private String outOfBoundsMsg(int index) {
  58. return "Index: " + index + ", Size: " + size;
  59. }

来自为知笔记(Wiz)

时间: 2024-12-28 20:51:06

Foundation Data Structure的相关文章

What is “passive data structure” in Android/Java?

From the Android developer web link: http://developer.android.com/reference/android/content/Intent.html, you can find that it says "It (Intent) is basically a passive data structure holding an abstract description of an action to be performed."

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[leedcode 211] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

[LeetCode] All O`one Data Structure 全O(1)的数据结构

Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string. Dec(Key) - If Key's value is 1, remove it from the data structu

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

211. Add and Search Word - Data structure design

就是trie 1 public class WordDictionary { 2 public class TrieNode { 3 public TrieNode[] child; 4 public char curChar; 5 public boolean isLeaf; 6 7 public TrieNode() { 8 child = new TrieNode[26]; 9 isLeaf = false; 10 } 11 } 12 13 TrieNode root; 14 15 pub