# 链表
❑ push (element):向链表尾部添加一个新元素。
❑ insert (element, position):向链表的特定位置插入一个新元素。
❑ getElementAt (index):返回链表中特定位置的元素。如果链表中不存在这样的元素,则返回 undefined。
❑ remove (element):从链表中移除一个元素。
❑ indexOf (element):返回元素在链表中的索引。如果链表中没有该元素则返回 - 1。
❑ removeAt (position):从链表的特定位置移除一个元素。
❑ isEmpty ():如果链表中不包含任何元素,返回 true,如果链表长度大于 0 则返回 false。
❑ size ():返回链表包含的元素个数,与数组的 length 属性类似。
❑ toString ():返回表示整个链表的字符串。由于列表项使用了 Node 类,就需要重写继承自 JavaScript 对象默认的 toString 方法,让其只输出元素的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| class Node { constructor(data) { this.value = data this.next = null } }
class LinkedList { head = null count = 0
push(element) { let node = new Node(element) if (this.head === null) { this.head = node } else { let current = this.head while (current.next) { current = current.next } current.next = node } this.count++ } removeAt(position) { if (position >= 0 && position < this.count) { let current = this.head if (position === 0) { this.head = this.head.next } else { let previous = null for (let i = 0; i < position; i++) { previous = current current = current.next } previous.next = current.next current.next = null } this.count-- return current } return; } insert(element, position) { if(position<0||position>this.count) return false let node = new Node(element) if (position === 0) { node.next = this.head this.head = node }else if(position === this.count){ this.push(element) }else{ let previous = this.head for(let i =0;i<position-1;i++){ previous = previous.next } node.next = previous.next previous.next = node } this.count++ return true } getElementAt(index){ if(index<0 || index>this.count) return undefined else{ let current = this.head for(let i=0;i<index-1;i++){ current = current.next } return current.value } } indexOf(element){ let current = this.head for(let i=0;i<this.count;i++){ if(this.isEqual(current.value,element)){ return i } current = current.next } return -1 } isEqual(a,b){ return JSON.stringify(a)===JSON.stringify(b) } remove(element){ const index = this.indexOf(element) return removeAt(index) } size(){ return this.count } isEmpty(){ return this.count===0 } toString(){ let str = '' let current = this.head for(let i=0;i<this.count;i++){ str+=String(current.value) current = current.next } return str } }
|
试一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const linkedlist = new LinkedList()
linkedlist.push('A') linkedlist.push('B') linkedlist.push('C') linkedlist.push('D') linkedlist.insert('E',2)
console.log(linkedlist) console.log(linkedlist.getElementAt(3)) console.log(linkedlist.getElementAt(5)) console.log(linkedlist.toString()) /{ "head": { "value": "A", "next": { "value": "B", "next": { "value": "E", "next": { "value": "C", "next": { "value": "D", "next": null } } } } }, "count": 5 } "E" "D" "ABECD" */
|