|
| 1 | +class Node { |
| 2 | + constructor(data) { |
| 3 | + this.data = data; |
| 4 | + this.next = null; |
| 5 | + this.prev = null; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +export default class DoublyList { |
| 10 | + constructor(data) { |
| 11 | + this.head = null; |
| 12 | + this.size = 0; |
| 13 | + } |
| 14 | + addFirst(data) { |
| 15 | + const newNode = new Node(data); |
| 16 | + if (this.head === null && this.size < 1) { |
| 17 | + this.head = newNode; |
| 18 | + } else { |
| 19 | + newNode.next = this.head; |
| 20 | + this.head.prev = newNode; |
| 21 | + this.head = newNode; |
| 22 | + } |
| 23 | + this.size++; |
| 24 | + } |
| 25 | + addLast(data) { |
| 26 | + const newNode = new Node(data); |
| 27 | + if (this.head === null && this.size < 1) { |
| 28 | + this.head = newNode; |
| 29 | + } else { |
| 30 | + let current = this.head; |
| 31 | + while (current.next.next !== null) { |
| 32 | + current = current.next; |
| 33 | + } |
| 34 | + current.next.next = newNode; |
| 35 | + newNode.prev = current.next; |
| 36 | + } |
| 37 | + this.size++; |
| 38 | + } |
| 39 | + insert(data, index) { |
| 40 | + const newNode = new Node(data); |
| 41 | + if (index < 0) { |
| 42 | + return; |
| 43 | + } else if (index === 0) { |
| 44 | + this.addFirst(newNode.data); |
| 45 | + } else if (index >= this.size) { |
| 46 | + this.addLast(newNode.data); |
| 47 | + } else { |
| 48 | + let iterator = 0; |
| 49 | + let current = this.head; |
| 50 | + while (iterator !== index - 1) { |
| 51 | + current = current.next; |
| 52 | + iterator++; |
| 53 | + } |
| 54 | + newNode.next = current.next; |
| 55 | + newNode.prev = current; |
| 56 | + current.next.prev = newNode; |
| 57 | + current.next = newNode; |
| 58 | + this.size++; |
| 59 | + } |
| 60 | + } |
| 61 | + searchNodeAt(index) { |
| 62 | + if (index < 0 || index > this.size) { |
| 63 | + return; |
| 64 | + } else if (index === 0) { |
| 65 | + return this.head; |
| 66 | + } else { |
| 67 | + let current = this.head; |
| 68 | + let iterator = 0; |
| 69 | + while (current.next !== null) { |
| 70 | + if (iterator === index) { |
| 71 | + return current; |
| 72 | + } |
| 73 | + current = current.next; |
| 74 | + iterator++; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + remove(index) { |
| 79 | + if (index < 0 || index >= this.size || this.size <= 0) { |
| 80 | + return; |
| 81 | + } else if (index === 0) { |
| 82 | + this.size--; |
| 83 | + if (this.size === 0) { |
| 84 | + this.head = null; |
| 85 | + } else { |
| 86 | + const temp = this.head.next; |
| 87 | + temp.prev = null; |
| 88 | + this.head = temp; |
| 89 | + } |
| 90 | + return; |
| 91 | + } else { |
| 92 | + let current = this.head; |
| 93 | + let iterator = 0; |
| 94 | + while (iterator !== index - 1) { |
| 95 | + current = current.next; |
| 96 | + iterator++; |
| 97 | + } |
| 98 | + if (current.next.next === null) { |
| 99 | + current.next = null; |
| 100 | + } else { |
| 101 | + current.next = current.next.next; |
| 102 | + current.next.prev = current; |
| 103 | + } |
| 104 | + } |
| 105 | + this.size--; |
| 106 | + } |
| 107 | + length() { |
| 108 | + return this.size; |
| 109 | + } |
| 110 | + print() { |
| 111 | + let iterator = this.head; |
| 112 | + while (iterator !== null) { |
| 113 | + console.log(iterator.data); |
| 114 | + iterator = iterator.next; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +let d = new DoublyList(); |
| 120 | +d.addFirst(1); |
| 121 | +d.addFirst(2); |
| 122 | +d.addFirst(3); |
| 123 | +d.addFirst(4); |
| 124 | +d.addFirst(5); |
| 125 | +d.remove(4); |
| 126 | +d.print(); |
| 127 | +console.log(d); |
0 commit comments