概念

  • 如生活中的购物清单,待办事项,都是列表
  • 元素不是很多
  • 不需要很长序列查找或者排序
  • 是最自然的数据组织方式

定义

  • 列表是一组有序数据,每个列表中的数据成为元素,元素的数量受内存控制
  • 不包含任何元素的列表称为空列表
  • 具有迭代器,比 for 更加灵活

实现

列表 - 图1

代码实现

  1. function List() {
  2. this.listLength = 0
  3. this.pos = 0
  4. this.dataStore = []
  5. this.add = add
  6. this.delete = del
  7. this.find = find
  8. this.change = change
  9. this.getItem = getItem
  10. this.insert = insert
  11. this.next = next
  12. this.prev = prev
  13. this.clear = clearList
  14. this.getLength = getLength
  15. this.getPos = getPos
  16. // 下面是第一次写缺少的方法
  17. this.toString = toString // 字符串返回当前list
  18. this.front = front // 直接到第一位
  19. this.end = end //到最后一个元素位置
  20. this.moveTo = moveTo //将当前位置移动到指定位置
  21. this.contains = contains //是否包含该元素,我认为跟find是一样的… 噢,find如果是0为返回值不好判断了,职能单一原则,还是需要分离开
  22. }
  23. function add(ob) {
  24. this.dataStore[this.listLength++] = ob
  25. }
  26. function del(ob) {
  27. // 找
  28. // 删
  29. // 改指针
  30. // 第一次实现忘了改 listLength
  31. var index = this.find(sourceObj)
  32. if (index > -1) {
  33. // 第一次用了 delete 数组用delete结果如下:
  34. // let arr = [1,2,3]
  35. // delete arr[2]
  36. // arr
  37. // (3) [1, 2, empty]
  38. // delete this.dataStore[index]
  39. this.dataStore.splice(index, 1) // 这样就能删掉了
  40. // slice方法不会改变原数组,
  41. if (this.pos >= index) {
  42. this.pos--
  43. }
  44. this.listLength--
  45. return true
  46. } else {
  47. return false
  48. }
  49. }
  50. function find(obj) {
  51. for (let i = 0; i < this.listLength; i++) {
  52. if (obj == this.dataStore[i]) {
  53. return i
  54. }
  55. }
  56. // 第一次实现这里没有职能单一原则,返回了false,需要跟contain区分开
  57. return -1
  58. }
  59. function change(sourceObj, targetObj) {
  60. var index = this.find(sourceObj)
  61. if (index) {
  62. this.dataStore.splice(index, 1, targetObj)
  63. return true
  64. } else {
  65. return false
  66. }
  67. }
  68. function getItem() {
  69. return this.dataStore[this.pos]
  70. }
  71. function insert(sourceObj, targetObj) {
  72. var index = this.find(sourceObj)
  73. if (index) {
  74. // 第一次实现index 没有 +1,需要+1
  75. this.dataStore.splice(index + 1, 0, targetObj)
  76. // 第一次实现没有改变listLength
  77. this.listLength++
  78. return true
  79. } else {
  80. return false
  81. }
  82. }
  83. function next() {
  84. // 第一次实现是小于 length -1
  85. // 这样的问题就是迭代器到不了头
  86. if (this.pos < this.listLength ) {
  87. this.pos++
  88. return true
  89. } else {
  90. return false
  91. }
  92. }
  93. function prev() {
  94. if (this.pos != 0) {
  95. this.pos--
  96. return true
  97. } else {
  98. return false
  99. }
  100. }
  101. function clearList() {
  102. this.dataStore = []
  103. this.listLength = this.pos = 0
  104. }
  105. function getLength() {
  106. return this.listLength
  107. }
  108. function getPos() {
  109. return this.pos
  110. }
  111. function toString() {
  112. return this.dataStore
  113. }
  114. function front() {
  115. this.pos = 0
  116. }
  117. function end() {
  118. this.pos = this.listLength > 0 ? this.listLength - 1 : 0
  119. }
  120. function moveTo(index) {
  121. this.pos = index > this.listLength - 1 ? this.listLength : index
  122. }
  123. function contains(obj) {
  124. if (this.find(obj) > -1) {
  125. return true
  126. }
  127. return false
  128. }
  129. var list = new List()
  130. list.add('嘿嘿')
  131. list.add('吼吼')
  132. list.add('试试')
  133. var log = console.log
  134. var warn = console.warn
  135. log(list.listLength)
  136. log(list.getItem())
  137. log(list.next())
  138. log(list.getItem())
  139. log(list.find('试试'))
  140. if (list.find('试试')) {
  141. log('找到试试了, 我要把它变成不想试试')
  142. list.change('试试', '不想试试')
  143. log(list.find('不想试试'))
  144. }
  145. for (list.front(); list.getPos() < list.getLength() ; list.next()) {
  146. warn(list.getItem())
  147. }
  148. list.insert('吼吼', '在吼吼后面插入')
  149. for (list.front(); list.getPos() < list.getLength() ; list.next()) {
  150. warn(list.getItem())
  151. }