1. // 节点类
    2. function Node(val) {
    3. this.val = val
    4. this.next = null
    5. }
    6. // 链表类
    7. function NodeList() {
    8. this.head = null
    9. this.length = 0
    10. // 向链表末尾追加元素
    11. this.push = push
    12. // 在值为 val 的节点后面添加一个值为 newVal 的节点
    13. this.insertAfter = insertAfter
    14. // 在链表中查找值为 val 的节点
    15. this.find = find
    16. // 在链表中查找值为val 的节点的前一个节点
    17. this.findPrev = findPrev
    18. // 删除链表中值为 val 的节点
    19. this.remove = remove
    20. // 将值为val 的节点修改为 newVal
    21. this.update = update
    22. // 打印链表
    23. this.toString = toString
    24. }
    25. function push(val) {
    26. let node = new Node(val)
    27. // 第一个节点
    28. if (this.head === null) {
    29. this.head = node
    30. } else {
    31. let current = this.head
    32. // 找到最后一个节点
    33. while (current.next !== null) {
    34. current = current.next
    35. }
    36. // 最后一个节点后面追加节点
    37. current.next = node
    38. }
    39. this.length++
    40. }
    41. function insertAfter(val, newVal) {
    42. let current = this.find(val)
    43. if (current === null) return -1
    44. let node = new Node(newVal)
    45. // 先将当前节点的指向给到新节点的指向 !
    46. node.next = current.next
    47. // 再将当前节点指向新节点
    48. current.next = node
    49. this.length++
    50. }
    51. function find(val) {
    52. // 空链表
    53. if (this.head === null) return null
    54. let current = this.head
    55. // 查找到最后一个节点之前
    56. while (current.next !== null) {
    57. if (current.val === val) return current
    58. current = current.next
    59. }
    60. // 判断最后1个节点
    61. if (current.val === val) return current
    62. return null
    63. }
    64. function findPrev(val) {
    65. // 空链表或者查找第一个节点的前一个节点均查不到
    66. if (this.head === null || this.head.val === val) return null
    67. let current = this.head
    68. // 最后一个节点不进循环, 最后一个节点不可能是某个节点的前一个节点
    69. while (current.next !== null) {
    70. if (current.next.val === val) return current
    71. current = current.next
    72. }
    73. return null
    74. }
    75. function remove(val) {
    76. let current = this.find(val)
    77. if (current === null) return -1
    78. else {
    79. let prev = this.findPrev(val)
    80. if (prev !== null) {
    81. prev.next = current.next
    82. } else {
    83. // prev === null 即删第一个节点 让当前节点的下一个节点第2个节点为头
    84. this.head = current.next
    85. }
    86. }
    87. this.length--
    88. }
    89. function update(val, newVal) {
    90. let current = this.find(val)
    91. if (current === null) return -1
    92. current.val = newVal
    93. }
    94. function toString() {
    95. let current = this.head
    96. let str = ''
    97. let index = 0
    98. while (current !== null) {
    99. // str += index + current.val + (current.next ? ' -> ' : '')
    100. str += `(${index}) ${current.val}${current.next ? ' -> ' : ''}`
    101. current = current.next
    102. index++
    103. }
    104. console.log(str)
    105. }
    106. // 测试代码
    107. let nodeList = new NodeList()
    108. nodeList.push('1st element')
    109. nodeList.push('2nd element')
    110. nodeList.push('3rd element')
    111. nodeList.push('4th element')
    112. console.log('find', nodeList.find('1st element'))
    113. console.log('find', nodeList.find('2nd element'))
    114. console.log('find', nodeList.find('3rd element'))
    115. console.log('find', nodeList.find('4th element'))
    116. console.log('findPrev', nodeList.findPrev('1st element'))
    117. console.log('findPrev', nodeList.findPrev('2nd element'))
    118. console.log('findPrev', nodeList.findPrev('3rd element'))
    119. console.log('findPrev', nodeList.findPrev('4th element'))
    120. nodeList.remove('4th element')
    121. console.log('length', nodeList.length, 'nodeList', nodeList)
    122. nodeList.insertAfter('2nd element', '2nd element 2')
    123. console.log('length', nodeList.length, 'nodeList', nodeList)
    124. nodeList.update('2nd element', '2nd element update')
    125. console.log('length', nodeList.length, 'nodeList', nodeList)
    126. nodeList.toString()
    127. // 原文链接:https://blog.csdn.net/aSmallProgrammer/article/details/106137774