使用js写一个链表
时间: 2023-07-20 10:03:40 浏览: 43
好的,这里是一个简单的链表实现:
```javascript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
}
append(data) {
const newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
this.length++;
}
insert(position, data) {
if (position < 0 || position > this.length) {
return false;
}
const newNode = new Node(data);
if (position === 0) {
newNode.next = this.head;
this.head = newNode;
} else {
let index = 0;
let current = this.head;
let previous = null;
while (index < position) {
previous = current;
current = current.next;
index++;
}
newNode.next = current;
previous.next = newNode;
}
this.length++;
return true;
}
removeAt(position) {
if (position < 0 || position >= this.length) {
return null;
}
let current = this.head;
if (position === 0) {
this.head = current.next;
} else {
let index = 0;
let previous = null;
while (index < position) {
previous = current;
current = current.next;
index++;
}
previous.next = current.next;
}
this.length--;
return current.data;
}
indexOf(data) {
let index = 0;
let current = this.head;
while (current !== null) {
if (current.data === data) {
return index;
}
current = current.next;
index++;
}
return -1;
}
remove(data) {
const index = this.indexOf(data);
return this.removeAt(index);
}
isEmpty() {
return this.length === 0;
}
size() {
return this.length;
}
toString() {
let current = this.head;
let string = "";
while (current !== null) {
string += current.data + " ";
current = current.next;
}
return string.trim();
}
}
```
这个链表实现了常用的操作,包括添加、插入、删除、查找等。你可以使用这个链表来存储数据,并进行各种操作。