javascript数据结构与算法笔记(六):双向链表
一:简介
双向链表和普通链表的区别在于,在链表中,一个节点只有链向下一个节点的链接,而在双向链表中,链接是双向的:一个链向下一个元素,另一个链向前一个元素
结构如下:
二:ES6版DoublyLinkedList类
1.DoublyLinkedList类声明以及辅助类Node存储节点信息
如想使用WeakMap类声明LinkedList类,具体原因可以参照:https://blog.csdn.net/wushichao0325/article/details/84969725
class Node{
constructor(element){
this.element=element;
this.next=null;//指向下一个
this.prev=null;//指向前一个
}
}
class DoublyLinkedList{
constructor(){
this.length=0;
this.head=null;
this.tail=null;
}
}
2.向链表尾部追加元素
append(element){
let node=new Node(element);
let current;
if(this.head==null){//如果插入的元素为头节点
this.head=node;
}else{
current=this.head;
//循环列表,知道找到最后一项
while(current.next){
current=current.next;
}
//找到最后一项,让最后一项的next指向将要添加的元素
current.next=node;
this.tail=node;
}
this.length++;//记录列表的最新长度
}
3.从链表中移除元素
移除元素也有两种场景:第一种是移除第一个元素,第二种是移除最后一个元素,第三种是移除中间的任一元素
情况一:
情况二:
情况三:
removeAt(position){
//检测传入的position是否越界
if(position>-1&&position<this.length){
let current=this.head;
let previous;//记录前一个元素
let index=0;
if(position==0){//移除第一项
this.head=current.next;
//如果只有一项,更新tail
if(this.length==1){
this.tail=null;
}else{
this.head.prev=null;
}
}else if(position==this.length-1){//最后一项
current=this.tail;
this.tail=current.prev;
this.tail.next=null;
}else{
while(index++<position){//找到position位置的前一个元素Node
previous=current;
current=current.next;//此时current指向的position位置的元素
}
//将前一个的next指针指向currrent即position位的next节点
previous.next=current.next;
current.next.prev=previous;
}
this.length--;
return current.element;//返回移除的position位的元素信息
}else{
return null;
}
}
4.在任意位置插入元素
任意位置插入元素也有两种场景:第一种是在列表的起点添加一个元素,第二种是在列表的末尾添加一个元素,第三种是在列表中间或尾部添加一个元素。
情景一:
情景二:
情景三:
insert(position,element){
if(position>=0&&position<=this.length){//==this.length保证可以往队尾插入
let node=new Node(element),
current=this.head,
previous,
index=0;
if(position==0){//在第一个位置插入
if(!this.head){
this.head=node;
this.tail=node;
}else{
node.next=current;
current.prev=node;
this.head=node;
}
}else if(position==this.length){//末尾
current=this.tail;
console.log("current:",current)
current.next=node;
node.prev=current;
this.tail=node;
}else{
while(index++<position){
previous=current;//记录插入位置的前一个节点
current=current.next;//记录插入位置的节点
}
node.next=current;
previous.next=node;
current.prev=node;
node.prev=previous;
}
this.length++;//更新链表的长度
return true;
}else{
return false;
}
}
5.打印链表
toString方法会把LinkedList对象转换成一个字符串
toString(){
let current=this.head;
let string='linklist:';
while(current){
string+=current.element+(current.next?'->':'');
current=current.next;
}
return string;
}
6.查询指定元素是否存在
indexOf方法接收一个元素的值,如果在列表中找到
它,就返回元素的位置,否则返回-1。
indexOf(element){//查询指定的元素是否存在在链表中
let current=this.head;
let index=-1;
while(current){
index++;
if(element==current.element){
return index;
}
current=current.next;
}
return -1;
}
7.isEmpty、size、getHead方法
isEmpty(){
return this.length==0;
}
size(){
return this.length;
}
getHead(){
return this.head;
}
8.删除指定元素
remove(element){
let index=this.indexOf(element);
return this.removeAt(index);
}
9.使用链表
let linkedList=new DoublyLinkedList();
linkedList.append("1");
linkedList.append("2");
console.log(linkedList.toString())
let result=linkedList.insert(2,"3");
console.log(linkedList.toString())
// console.log(linkedList.toString());
linkedList.remove("2");
console.log(linkedList.toString());
console.log(linkedList.indexOf("1"));
// linkedList.removeAt(0);
// console.log(linkedList.toString());
// console.log(linkedList.indexOf("1"))