<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script>
class Node {
constructor(ele) {
this.element = ele;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
append(element) {
let node = new Node(element);
if (this.head === null) {
this.head = node;
} else {
// console.log(node, this.head);
let current = this.getNode(this.size - 1, this.size);
current.next = node;
}
this.size++;
}
getNode(index,) {
if (index < 0 || index >= this.size) {
console.log("aa");

throw new Error("out range");
}
let current = this.head;
for (let i = 0; i < index; i++) {
// 对象的引用关系 循环给next属性去取值
current = current.next;
console.log(size, current);
}
return current;
}
appendAt(postion,) {
if (postion < 0 || postion > this.size) {
throw new Error("postion out rangs");
}
let node = new Node(element);
if (postion === 0) {
node.next = this.head;
this.head = node;
} else {
let pre = this.getNode(postion - 1);
node.next = pre.next;
pre.next = node;
}
this.size++;
}
removeAt() {}
indexOf(element) {}
}
let li = new LinkedList();
li.append(1);
li.append(2);
li.append(3);
// li.appendAt(3, 4);
// li.append(3);
li.append(4);
console.log(li);
let obj = {
index: 1,
child: {},
};
for (let i = 0; i < 1; i++) {
obj = obj.child;
}
console.log(obj);
</script>
</head>
<body></body>
</html>