"먼저 그들의 책임을 살펴 보자"
- push (element) : 연결 목록 끝에 새 요소를 추가합니다.
- insert (element, position) : 연결 목록의 특정 위치에 새 요소를 삽입합니다.
- getElementAt (index) : 연결된 목록의 특정 위치에있는 요소를 반환합니다. 연결 목록에 이러한 요소가 없으면 undefined가 반환됩니다.
- remove (element) : 연결된 목록에서 요소를 제거합니다.
- indexOf (element) : 연결 목록에있는 요소의 인덱스를 반환합니다. 연결 목록에 이러한 요소가 없으면 -1이 반환됩니다.
- removeAt (position) : 연결된 목록의 특정 위치에서 요소를 제거합니다.
- isEmpty () : 연결 목록에 요소가 포함되어 있지 않으면 true를 반환하고 연결 목록의 길이가 0보다 크면 false를 반환합니다.
- size () : 배열의 길이 속성과 유사하게 연결된 목록에 포함 된 요소의 수를 반환합니다.
- toString () : 전체 연결 목록을 나타내는 문자열을 반환합니다. 목록 항목은 Node 클래스를 사용하기 때문에 요소의 값만 출력하도록 JavaScript 객체에서 상속 된 기본 toString 메서드를 재정의해야합니다.
export function defaultEquals(a, b) {
return a === b;
}
export class Node {
constructor(element) {
this.element = element;
this.next = undefined;
}
}
export default class LinkedList {
constructor(equalsFn = defaultEquals) {
this.count = 0; // {2}
this.head = undefined; // {3}
this.equalsFn = equalsFn; // {4}
}
push(element) {
const node = new Node(element); // {1}
let current; // {2}
if (this.head == null) { // {3}
this.head = node;
} else {
current = this.head; // {4}
while (current.next != null) { // {5} 获得最后一项
current = current.next;
}
// 将其next赋为新元素,建立链接
current.next = node; // {6}
}
this.count++; // {7}”
}
removeAt(index) {
// 检查越界值
if (index >= 0 && index < this.count) { // {1}
let current = this.head; // {2}
// 移除第一项
if (index === 0) { // {3}
this.head = current.next;
} else {
let previous; // {4}
for (let i = 0; i < index; i++) { // {5}
previous = current; // {6}
current = current.next; // {7}
}
// 将previous与current的下一项链接起来:跳过current,从而移除它
previous.next = current.next; // {8}
}
this.count--; // {9}
return current.element;
}
return undefined; // {10}
}
getElementAt(index) {
if (index >= 0 && index <= this.count) { // {1}
let node = this.head; // {2}
for (let i = 0; i < index && node != null; i++) { // {3}
node = node.next;
}
return node; // {4}
}
return undefined; // {5}
}
insert(element, index) {
if (index >= 0 && index <= this.count) { // {1}
const node = new Node(element);
if (index === 0) { // 在第一个位置添加
const current = this.head;
node.next = current; // {2}
this.head = node;
} else {
const previous = this.getElementAt(index - 1); // {3}
const current = previous.next; // {4}
node.next = current; // {5}
previous.next = node; // {6}
}
this.count++; // 更新链表的长度
return true;
}
return false; // {7}
}
indexOf(element) {
let current = this.head; // {1}
for (let i = 0; i < this.count && current != null; i++) { // {2}
if (this.equalsFn(element, current.element)) { // {3}
return i; // {4}
}
current = current.next; // {5}
}
return -1; // {6}
}
remove(element) {
const index = this.indexOf(element);
return this.removeAt(index);
}
size() {
return this.count;
}
isEmpty() {
return this.size() === 0;
}
getHead() {
return this.head;
}
toString() {
if (this.head == null) { // {1}
return '';
}
let objString = `${this.head.element}`; // {2}
let current = this.head.next; // {3}
for (let i = 1; i < this.size() && current != null; i++) { // {4}
objString = `${objString},${current.element}`;
current = current.next;
}
return objString; // {5}
}
}