线性表 - 无序表
    getItem O(1)
    setItem O(1)
    push O(n)
    insert O(n)
    delect O(n)
    search O(n)

    基本操作

    1. class List {
    2. // 1.创建空的数组 指定size
    3. constructor() {
    4. this.arr = new Array(10);
    5. this.size = 10;
    6. this.length = 0;
    7. this.realloc = () => {
    8. let new_arr = null;
    9. // 扩大
    10. if(this.length === this.size) {
    11. // 增加空间 原来数据拷贝到新数据
    12. this.size = this.size*2;
    13. new_arr = new Array(this.size);
    14. } else if(this.length < (this.size/4)) {
    15. // 判断是否需要缩小
    16. this.size = Math.floor(this.size/2);
    17. new_arr = new Array(this.size);
    18. }
    19. if(new_arr) {
    20. for(let i = 0; i < this.length; i++){
    21. new_arr[i] = this.arr[i]
    22. }
    23. delete this.arr;
    24. this.arr = new_arr;
    25. }
    26. };
    27. this.checkRange= (index) => {
    28. if(typeof index !== 'number' || index < 0 || index >= this.length) {
    29. throw new RangeError();
    30. }
    31. }
    32. }
    33. getItem(index){
    34. this.checkRange(index);
    35. return this.arr[index];
    36. }
    37. setItem(index, value) {
    38. this.checkRange(index);
    39. return this.arr[index] = value;
    40. }
    41. // 添加
    42. append(value){
    43. this.arr[this.length] = value;
    44. this.length++;
    45. // 如果超出 扩展空间
    46. this.realloc();
    47. }
    48. // 插入
    49. insert(value, pointIndex) {
    50. if(pointIndex < 0 || pointIndex > this.length) {
    51. throw new RangeError();
    52. } else if (pointIndex === this.length) {
    53. append(value);
    54. } else {
    55. // 1.index的数据都向后移动一位
    56. for(let j = this.length; j > pointIndex; j --) {
    57. this.arr[j] = this.arr[j-1];
    58. }
    59. // 2.新的数据放到 index 位置上
    60. this.arr[pointIndex] = value;
    61. this.length++;
    62. // 如果超出 扩展空间
    63. this.realloc();
    64. }
    65. }
    66. // 删除某一项
    67. delete(index) {
    68. this.checkRange(index);
    69. // 1.index的数据都向前移动一位
    70. for(let j = index; j < this.length -1; j ++) {
    71. this.arr[j] = this.arr[j+1];
    72. }
    73. this.length--;
    74. this.realloc();
    75. }
    76. // 有序的数组 二分法查找 适用于搜索性能更高
    77. search(value) {
    78. let s = 0;
    79. let e = this.arr.length - 1;
    80. while(s <= e) {
    81. let center = Math.floor((s+e)/2);
    82. if(value === this.arr[center]) {
    83. return center;
    84. } else if(value < this.arr[center]) {
    85. e = center - 1;
    86. } else(value > this.arr[center]) {
    87. s = center + 1;
    88. }
    89. }
    90. return -1;
    91. }
    92. }
    93. const list = new List();
    94. list.append(1);
    95. list.append(2);
    96. list.append(3);
    97. list.append(4);
    98. list.append(5);
    99. list.append(6);
    100. list.append(7);
    101. list.append(8);
    102. list.append(9);
    103. list.append(10);
    104. list.append(6);
    105. list.append(7);
    106. list.append(8);
    107. list.append(9);
    108. list.append(10);
    109. list.append(11);
    110. console.log(list);