第一部分:封装单向链表
// 链表中节点的结构
class Lnode {
constructor(data) {
this.data = data;
this.next = null;
}
}
// 链表的结构
class LinkList {
constructor() {
this.head = null;
this.len = 0
}
// 在链表最后添加新节点
append(ele) {
let node = new Lnode(ele);
if (this.head == null) {
this.head = node;//添加新节点
} else {//不为空
// 找到最后一个节点
let current = this.head;
while (current.next != null) {
current = current.next;
}
// 最后一个节点
current.next = node;
}
this.len++;
}
//指定位置插入元素
insert(position, ele) {
if (position < 0 || position > this.len || !Number.isInteger(position)) return false;
let node = new Lnode(ele);
if (position == 0) {//头部添加元素
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;//新节点指向头部节点
this.head = node;//指向新节点
}
this.len++;
} else if (position == this.len) {//尾巴添加元素
this.append(ele);
} else {//指定位置添加元素
let index = 0;
let current = this.head;
while (index < position - 1) {//查找前一位
// 继续查找
current = current.next;
index++;
}
// 指向下一位
node.next = current.next;
current.next = node;//指向新节点
this.len++;
}
}
// 指定位置移除元素
removeAt(position) {
if (position < 0 || position > this.len - 1 || !Number.isInteger(position)) return false;
if (position == 0) {//移除头部元素
this.head = this.head.next;//指向下一个
} else {
let current = this.head;
let index = 0;
while (index < position - 1) {//查找前一位
current = current.next;// 继续查找
index++;
}
current.next = current.next.next;//指向下一个节点.next
}
this.len--;
}
// 查找元素的位置
indexOf(ele) {
let current = this.head;
let index = 0;
while (index < this.len) {
if (current.data == ele) {
return index;
} else {
current = current.next;
index++;
}
}
return -1;
}
// 移除指定的元素
remove(ele) {
let in1 = this.indexOf(ele);//查找指定位置
this.removeAt(in1);//移除
}
toString() {
let current = this.head;
let index = 0;
let res = "";
while (index < this.len) {
res += "-" + current.data;//添加元素
current = current.next;
index++;
}
return res.slice(1);//从下标1开始截取
}
}
let list = new LinkList();
// list.append("hello");
for (let i = 0; i < 4; i++) {
list.append(i);
}
list.insert(2, 2);//0 1 hello 2 3
list.insert(2, 2);//0 1 hello 2 3
// list.removeAt(2);//0 1 3
// console.log(list.indexOf("hello")); //-1
// console.log(list.indexOf(2)); //2
// list.remove(2);//0 1 3
// list.insert(2, "hello");//0 1 hello 3
// console.log(list.toString());//0-1-2-3
console.log(list);
第二部分:封装双向链表
class Dnode {
constructor(data) {
this.pre = null;
this.data = data;
this.next = null;
}
}
class DoubleLinkList {
constructor() {
this.head = null;
this.tail = null;
this.len = 0;
}
append(ele) {
let node = new Dnode(ele);
if (this.len == 0) {
this.head = node;//头节点指向新节点
this.tail = node;//尾节点指向新节点
} else {
node.pre = this.tail;
this.tail.next = node;
this.tail = node;
}
this.len++
}
insert(position, ele) {
if (position < 0 || position > this.len || !Number.isInteger(position)) return false
let node = new Dnode(ele);
if (position == 0) {
if (this.len == 0) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head.pre = node;
this.head = node;
}
this.len++;
} else if (position == this.len) {
this.append(ele);
} else {
let current = this.head, index = 0;
while (index < position - 1) {
current = current.next;
index++;
}
// 新节点练上去
node.pre = current;
node.next = current.next;
current.next = node;
node.next.pre = node;
this.len++;
}
}
// 3.removeAt() 移除指定位置的元素
removeAt(position) {
if (position < 0 || position > this.len - 1 || !Number.isInteger(position)) return false
if (position == 0) {
this.head = this.head.next;
this.head.pre = null;
} else if (position == this.len - 1) {
this.tail = this.tail.pre;
this.tail.next = null;
} else {
let current = this.head, index = 0;
while (index < position - 1) {
current = current.next;
index++;
}
current.next = current.next.next;
current.next.pre = current;
}
this.len--;
}
// 4.indexOf(ele) 查找元素的位置
indexOf(ele) {
let current = this.head;
let index = 0;
while (index < this.len) {
if (current.data == ele) {
return index;
} else {
current = current.next;
index++;
}
}
return -1;
}
// 5.remove(ele) 移除指定的元素
remove(ele) {
let i1 = this.indexOf(ele);
this.removeAt(i1);
}
toAfterString() {
let current = this.head;
let index = 0;
let res = "";
while (index < this.len) {
res += '-' + current.data;
current = current.next;
index++;
}
return res.slice(1);
}
toBeforeString() {
let current = this.tail, index = this.len - 1, res = "";
while (index >= 0) {
res += '-' + current.data;
current = current.pre;
index--;
}
return res.slice(1);
}
}
let dlist = new DoubleLinkList();
for (let i = 0; i < 4; i++) {
dlist.append(i);
}
dlist.insert(2, "hello");//0 1 hello 2 3
// dlist.removeAt(2);//0 1 3
// console.log(dlist.indexOf(2)); //2
// dlist.remove(2);//0 1 3
// dlist.insert(2,"hello");//0 1 hello 3
// console.log(dlist.toAfterString());//0-1-2-3
// console.log(dlist.toBeforeString());//3-2-1-0