单链表

  1. function LinkedList() {
  2. // Node辅助类,表示要加入列表的项,element是即将添加到列表的值,next是指向列表中下一个节点项的指针
  3. let Node = function (element) {
  4. this.element = element
  5. this.next = null
  6. }
  7. let length = 0
  8. let head = null
  9. // 向链表尾部追加元素
  10. this.append = function (element) {
  11. let node = new Node(element)
  12. let current
  13. if (head === null) { // 列表中第一个节点
  14. head = node
  15. } else {
  16. current = head
  17. while (current.next) {
  18. current = current.next // 找到最后一项,是null
  19. }
  20. current.next = node // 给最后一项赋值
  21. }
  22. length++ // 更新列表的长度
  23. }
  24. // 从链表中移除指定位置元素
  25. this.removeAt = function (position) {
  26. if (position > -1 && position < length) { // 值没有越界
  27. let current = head
  28. let previous, index = 0
  29. if (position === 0) { // 移除第一项
  30. head = current.next
  31. } else {
  32. while (index++ < position) {
  33. previous = current
  34. current = current.next
  35. }
  36. previous.next = current.next // 将previous与current的下一项连接起来,跳过current,从而移除
  37. }
  38. length-- // 更新列表的长度
  39. return current.element
  40. } else {
  41. return null
  42. }
  43. }
  44. // 在链表任意位置插入一个元素
  45. this.insert = function (position, element) {
  46. if (position >= 0 && position <= length) { // 检查越界值
  47. let node = new Node(element),
  48. current = head,
  49. previous,
  50. index = 0
  51. if (position === 0) { // 在第一个位置添加
  52. node.next = current
  53. head = node
  54. } else {
  55. while (index++ < position) {
  56. previous = current
  57. current = current.next
  58. }
  59. node.next = current // 在previous与current的下一项之间插入node
  60. previous.next = node
  61. }
  62. length++
  63. return true
  64. } else {
  65. return false
  66. }
  67. }
  68. // 把链表内的值转换成一个字符串
  69. this.toString = function (separator = ' ') {
  70. let current = head,
  71. string = ''
  72. while (current) {
  73. string += current.element + separator
  74. current = current.next
  75. }
  76. let lastSeparator = string.lastIndexOf(separator);
  77. return string.substr(0, lastSeparator);
  78. }
  79. // 在链表中查找元素并返回索引值
  80. this.indexOf = function (element) {
  81. let current = head,
  82. index = 0
  83. while (current) {
  84. if (element === current.element) {
  85. return index
  86. }
  87. index++
  88. current = current.next
  89. }
  90. return -1
  91. }
  92. // 从链表中移除指定元素
  93. this.remove = function (element) {
  94. let index = this.indexOf(element)
  95. return this.removeAt(index)
  96. }
  97. this.isEmpty = function () {
  98. return length === 0
  99. }
  100. this.size = function () {
  101. return length
  102. }
  103. this.getHead = function () {
  104. return head
  105. }
  106. }
  107. let list = new LinkedList()
  108. list.append(1)
  109. list.append(2)
  110. console.log(list.toString('->')) // 1 2
  111. list.insert(0, 'hello')
  112. list.insert(1, 'world')
  113. console.log(list.toString()) // hello world 1 2
  114. list.remove(1)
  115. list.remove(2)
  116. console.log(list.toString()) // hello world