getElementAt(position) { if (position < 0 || position >= this.length) returnundefined; let current = this.head; for (let i = 0; i < position; i++) { current = current.next; } return current; }
向链表尾部添加元素
向链表尾部添加一个元素时,可能有两种场景:
场景1:链表为空,那添加的就是第一个元素
场景2:链表不为空
实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
push(element) { let node = newNode(element); let current; if (this.head === undefined) { this.head = node; } else { current = this.head; while (current.next != null) { current = current.next; } current.next = node; } this.length++; }
/** * 向链表尾部添加节点 * @paramelement */ push(element) { let node = newNode(element); let current; if (this.head === undefined) { this.head = node; } else { current = this.head; while (current.next != null) { current = current.next; } current.next = node; } this.length++; }
/** * 在链表的指定位置插入节点 * @paramposition * @paramelement */ insert(position, element) { // position不能超出边界值 if (position < 0 || position > this.length) returnfalse; let node = newNode(element); if (position === 0) { node.next = this.head; this.head = node; } else { let previous = this.getElementAt(position - 1); let current = previous.next; node.next = current; previous.next = node; } this.length++; returntrue; }
/** * 删除链表中指定位置的元素,并返回这个元素的值 * @paramposition */ removeAt(position) { if (position < 0 || position >= this.length) returnundefined; let current = this.head; if (position === 0) { this.head = current.next; } else { let previous = this.getElementAt(position - 1); current = previous.next; previous.next = current.next; } this.length--; return current.element; }
/** * 删除链表中对应的元素 * @paramelement */ remove(element) { let index = this.indexOf(element); returnthis.removeAt(index); }
/** * 在链表中查找给定元素的索引 * @paramelement */ indexOf(element) { let current = this.head; for (let i = 0; i < this.length; i++) { if (current.element === element) return i; current = current.next; } return -1; }
/** * 返回链表中索引所对应的元素 * @paramposition */ getElementAt(position) { if (position < 0 || position >= this.length) returnundefined; let current = this.head; for (let i = 0; i < position; i++) { current = current.next; } return current; }
insert(element, index) { if (index >= 0 && index <= this.length) { let node = newNode(element); let current = this.head; if (index === 0) { if (this.head === null) { // 如果链表为空 this.head = node; this.tail = node; } else { node.next = this.head; current.pre = node; this.head = node; } } elseif (index === this.length) { current = this.tail; current.next = node; node.pre = current; this.tail = node; } else { let previous = this.getElementAt(index - 1); current = previous.next; node.next = current; previous.next = node; current.pre = node; node.pre = previous; } this.length++; returntrue; } returnfalse }
removeAt(position) { // position不能超出边界值 if (position < 0 || position >= this.length) returnnull; let current = this.head; let previous; if (position === 0) { // 移除头部元素 this.head = current.next; this.head.pre = null; if (this.length === 1) this.tail = null; } elseif (position === this.length - 1) { // 移除尾部元素 current = this.tail; this.tail = current.pre; this.tail.next = null; } else { // 移除中间元素 current = this.getElementAt(position); previous = current.pre; previous.next = current.next; current.next.pre = previous; } this.length--; return current.element; }
getTail() { returnthis.tail; }
toString() { let current = this.head; let s = '';
while (current) { let next = current.next; let previous = current.prev; next = next ? next.element : 'null'; previous = previous ? previous.element : 'null'; s += `[element: ${current.element}, prev: ${previous}, next: ${next}] `; current = current.next; }
return s; } }
我们写测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
let doubleLinkedList = newDoubleLinkedList(); doubleLinkedList.push(1); doubleLinkedList.push(3); doubleLinkedList.push(5); doubleLinkedList.push(7); doubleLinkedList.push(9);
let node = newNode(element); if (this.head === undefined) { this.head = node; } else { let current = this.getElementAt(this.length - 1); current.next = node; } node.next = this.head; this.length++; }
insert(position, element) { if (position < 0 || position >= this.length) returnfalse; let node = newNode(element); if (position === 0) { node.next = this.head; let current = this.getElementAt(this.length - 1); current.next = node; this.head = node; } else { let previous = this.getElementAt(position - 1); node.next = previous.next; previous.next = node; } this.length++; returntrue; }
removeAt(position) { if (position < 0 || position >= this.length) returnfalse; let current = this.head; if (position === 0) { this.head = current.next; } else { let previous = this.getElementAt(position - 1); current = previous.next; previous.next = current.next; } this.length--; if (this.length > 1) { let last = this.getElementAt(this.length - 1); last.next = this.head; } return current.element; }
toString() { let current = this.head; let s = '';
for (let i = 0; i < this.length; i++) { let next = current.next; next = next ? next.element : 'null'; s += `[element: ${current.element}, next: ${next}] `; current = current.next; }
return s; } }
我们来写几个CircularLinkedList测试用例:
1 2 3 4 5 6 7 8 9 10 11
let circularLinkedList = newCircularLinkedList(); circularLinkedList.push(1); circularLinkedList.push(3); console.log(circularLinkedList.toString());