# 双向链表
😫太麻烦了 next😁next😅next😐next😖再也不想写第二遍了
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 120
| 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 } } 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 this.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 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
| class DoublyNode extends Node{ constructor(element){ super(element) this.prev = null } }
class DoublyLinkedList extends LinkedList{ constructor(){ super() this.tail = null } push(element){ const node = new DoublyNode(element) if(this.head===null){ this.head=node this.tail=node }else{ this.tail.next=node node.prev=this.tail this.tail=node } this.count++ } insert(element,position){ const node = new DoublyNode(element) if(position<0||position>this.count) return false; else if(position===0 && this.head===null){ this.head=node this.tail=node }else if(position===0 && this.head){ this.head.prev=node node.next=this.head this.head=node }else if(position===this.count){ this.tail.next=node node.prev=this.tail this.tail=node }else{ const previous = this.getElementAt(position-1) let current = previous.next node.next = current current.prev=node previous.next = node node.prev=previous } this.count++ return true } removeAt(){ } getHead(){ } }
const Db = new DoublyLinkedList() Db.push(1) Db.push(5) Db.push(7) Db.insert(10,1)
|
![image-20230302093305540]()