1、定义一个链表的结构
class Node {
constructor(element) {
this.element = element;
this.next = undefined;
}
}
2、定义一个LinkedList类,属性为链表的长度和头节点
class LinkedList {
constructor() {
this.count = 0;//长度
this.head = undefined;
}
}
3、向链表中添加元素
链表只能向链表的末尾添加元素,所以我们需要首先找到链表的末尾节点,可以用while循环,找到后将末尾节点的next指向添加的元素节点。
push(ele) {
const node = new Node(ele);
if (this.head === undefined) {
this.head = node;
}else {
let current = this.head;//current是一个指针
while (current.next != undefined) {
current = current.next;
}
current.next = node;
}
this.count++;
}
4、获取指定位置的节点
获取指定位置的系欸但,就用一个for循环,当i === index时,就返回此时的current
//获取指定位置的元素
getElementAt(index) {
if (this.isEmpty() ||index > this.count || index < 0) {
return undefined;
}
let current = this.head;
for(let i = 0; i < index; i++) {
current = current.next;
}
return current;
}
5、在指定位置插入节点
在指定位置插入节点,需要用到上面的getElementAt(index)方法。先找到指定位置的上一个位置的节点pre,将pre.next节点先保存到一个tmp中,然后将当前要插入的节点current赋值给pre.next节点,最后把tmp赋值给current.next。
//在指定位置插入元素
insert(ele, index) {
let current = new Node(ele);
if (index === 0) {
current.next = this.head;
this.head = current;
} else {
//找到指定位置的前一个位置
let previous = this.getElementAt(index - 1);
//将前一个元素的next赋值给要插入的元素
current.next = previous.next;
//将插入元素的node赋值给前一个元素的next
previous.next = current;
}
this.count++;
}
6、移除指定位置的元素
也是先找到指定位置前一个位置的节点pre,然后将pre.next === pre.next.next。
//移除指定位置的元素
removeAt(index) {
let current = this.head;
if (index === 0) {
this.head = current.next;
} else {
//找到这个元素和这个位置之前的元素
let previous = this.getElementAt(index - 1);
current = previous.next;
//将这个元素的next赋值给这个元素之前元素的next
previous.next = current.next;
}
this.count--;
//返回要移除的元素位置
return current.element;
}
7、返回指定节点的位置
//返回指定元素的位置
indexOf(element) {
let current = this.head;
for(let i = 0;i < this.size()&¤t != undefined;i++) {
if (current.element === element) {
return i;
}
current = current.next;
}
return -1;
}
8、将链表转换为字符串输出
将链表转换为字符串输出,主要用到了模板字符串和for循环
//转换为字符串输出
toString() {
if(this.head == undefined) {
return '';
}
let objString = `${this.head.element}`;
let current = this.head.next;
for(let i = 1; i < this.size()&¤t != undefined; i++) {
objString = `${objString},${current.element}`;
current = current.next;
}
return objString;
}
9、获取链表长度,判断是否为空等
// 是否为空
isEmpty() {
return this.size() === 0;
}
// 长度
size() {
return this.count;
}
// 获取表头
getHead() {
return this.head;
}
// 清空链表
clear() {
this.head = undefined;
this.count = 0;
}
10、总结
对于链表的操作来说,最重要的就是使用循环,并且用一个指针current来指向当前的节点,针对不同的判断来进行一些操作。
本文正在参与「掘金 2021 春招闯关活动」, 点击查看 活动详情