push(element) { const node = new Node(element) let current if (this.head == null) { this.head = node } else { current = this.head while(current.next != null) { current = current.next } current.next = nide } this.count ++ }
链表中移除元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
removeAt(index) { if (index >= 0 && index < this.count ) { let current = this.head if (index === 0) { this.head = current.next } else { let previous for (let i = 0; i < index; i ++) { previous = current current = current.next } previous.next = current.next } this.count -- return current.element } returnundefined }
循环迭代链表直到找到目标位置
1 2 3 4 5 6 7 8 9 10
getElementAt(index) { if (index >= 0 && index < this.count ) { let current = this.head for(let i = 0; i < index; i++) { current = current.next } return current } returnundefined }
重构下remove方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
removeAt(index) { if (index >= 0 && index < this.count ) { let current = this.head if (index === 0) { this.head = current.next } else { let previous = this.getElementAt(index) current = previous.next previous.next = current.next } this.count -- return current.element } returnundefined }
在任意位置插入元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
insert(element, index) { if (index >= 0 && index < this.count) { const node = new Node(element) if (index === 0) { const current = this.head node.next = current this.head = node } else { const previous = this.getElementAt(index - 1) const current = previous.next node.next = current previous.next = node } this.count ++ returntrue } returnfalse }
返回一个元素的位置
1 2 3 4 5 6 7 8 9 10
indexOf(element) { const current = this.head for (let i = 0; i < this.count && current != null; i++) { if (this.equalsFn(element, current.element)) { return i } current = current.next } return-1 }
从链表中移除元素
1 2 3 4
remove(element) { const index = this.indexOf(element); returnthis.removeAt(index) }
removeAt(index) { if (index >= 0 && index <= this.count) { let current = this.head if (index === 0) { if (this.size() === 1) { this.head = undefined } else { const removed = this.head current = this.getElementAt(this.size()) this.head = this.head.next current.next = this.head current = removed } } else { const previous = this.getElementAt(index - 1) current = previous.next previous.next = current.next } this.count -- return current.element } returnundefined }
有序链表
保持元素有序的链表结构,除了使用排序算法之外,我们还阔以将元素插入到正确的位置来保证链表的有序性
声明SortedLinkedList类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
exportconst Compare = { LESS_THAN: -1, BIGGER_THAN: 1, EQUALS: 0 } exportfunctiondefaultCompare(a, b) { if (a === b) { return Compare.EQUALS } return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN } exportclassSortedLinkedListextendsLinkedList{ constructor(equalsFn = defaultEquals, compareFn = defaultCompare) { super(equalsFn) this.equalsFn = equalsFn; this.compareFn = compareFn } }
有序插入元素
会用下面代码覆盖insert方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
getIndexNextSortedElement(element) { let current = this.head let i = 0; for (; i < this.size() && current; i ++) { const comp = this.compareFn(element, current.element) if (comp === Compare.LESS_THAN) { return i } current = current.next } return i } insert(element, index = 0) { if (this.isEmpty()) { returnsuper.insert(element, index === 0 ? index : 0) } const pos = this.getIndexNextSortedElement(element) returnsuper.insert(element, pos) }