if (!this.head) { this.head = node } else { let current = this.head while (current.next) { current = current.next } current.next = node }
this.length += 1 }
insert(position, data) { if (position < 0 || position > this.length) { returnfalse }
const node = new Node(data) if (position === 0) { node.next = this.head this.head = node } else { let current = this.head let prev = null let index = 0 while (index++ < position) { prev = current current = current.next } node.next = current prev.next = node } this.length += 1 returntrue }
get(position) { if (position < 0 || position >= this.length) returnnull let index = 0 let current = this.head while (index++ < position) { current = current.next } return current.data }
indexOf(data) { let current = this.head let index = 0
while (current) { if (current.data === data) { return index } current = current.next index += 1 }
return-1 }
update(position, data) { if (position < 0 || position >= this.length) returnfalse let index = 0 let current = this.head while (index++ < position) { current = current.next } current.data = data returntrue }
removeAt(position) { if (position < 0 || position >= this.length) { returnfalse }
if (position === 0) { this.head = this.head.next } else { let index = 0 let current = this.head let prev = null while (index++ < position) { prev = current current = current.next }
prev.next = current.next } this.length -= 1
returntrue }
remove(data) { const position = this.indexOf(data) returnthis.removeAt(position) }
size() { returnthis.length }
isEmpty() { return !this.length }
toString() { let current = this.head let str = '' while (current) { str += current.toString() + ',' current = current.next } return str } }
let current = this.head let index = 0 while (index++ < position) { current = current.next } node.next = current node.prev = current.prev current.prev.next = node current.prev = node this.length += 1 returntrue }
getNode(position) { if (position < 0 || position >= this.length) returnnull
const flag = this.length / 2 > position if (flag) { let index = 0 let current = this.head while (index++ < position) { current = current.next } return current } else { let index = this.length let current = this.tail while (index-- < position) { current = current.next } return current } }