数据结构——双向链表

一、双向链表

(1)概念:

单向链表

  • 只能从头遍历到尾或者从尾遍历到头(一般从头到尾)  即链表相连的过程是单向的

  • 实现的原理:上一个链表中有一个指向下一个的引用

  • 单向链表有一个比较明显的缺点:

    • 回到前一个节点是很难的 但在实际开发中, 经常会遇到需要回到上一个节点的情况

双向链表

  • 既可以从头遍历到尾, 又可以从尾遍历到头  即链表相连的过程是双向的

  • 实现的原理:一个节点既有向前连接的引用, 也有一个向后连接的引用

(2)双向链表的优缺点

优点:

  • 双向链表可以有效的解决单向链表的缺点(回到上一个节点)

缺点:

  • 每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 也就是实现起来要困难一些

  • 并且相当于单向链表, 必然占用内存空间更大一些

(3)双向列表的结构:

二、程序封装双向链表 

1.创建链表类

//节点的结构
class Dnode {
            constructor(data) {
                this.prev = null;
                this.data = data;
                this.next = null;
            }
        }
//链表的结构
class DoubleLinkList {
            constructor() {
                this.head = null;
                this.tail = null;
                this.length = 0
            }
        }

2.双向链表的操作

方法:

1)在尾部添加节点

append(ele) {
                let newnode = new Dnode(ele);
                // console.log(newnode);
                if (this.length == 0) { //空链表
                    this.head = newnode;
                    this.tail = newnode;
                } else {
                    newnode.prev = this.tail; //新节点.prev = 尾节点
                    this.tail.next = newnode; //尾节点.next指向新节点
                    this.tail = newnode; //将尾节点指向新节点
                }
                this.length++;
            }

2)向指定位置插入元素 

insert(position, ele) {
                // 位置是否合法
                if (position < 0 || position > this.length || !Number.isInteger(position)) {
                    return false
                }
                let newnode = new Dnode(ele);
                if (position == 0) {
                    if (this.length == 0) { //空链表
                        this.head = newnode;
                        this.tail = newnode;
                    } else {
                        newnode.next = this.head; //新节点.next指向头节点
                        this.head.prev = newnode; //头节点.prev指向新节点
                        this.head = newnode; //头节点指向新节点
                    }
                    this.length++
                } else if (position == this.length) {
                    this.append(ele)
                } else {
                    let current = this.head,
                        index = 0;

                    while (index < position - 1) {
                        current = current.next;
                        index++;
                    }
                    // 1.将新节点练上去
                    newnode.prev = current;
                    newnode.next = current.next;
                    // 2.
                    current.next = newnode;
                    newnode.next.prev = newnode;
                    this.length++;
                }

            }

3)移除指定位置的节点

removeAt(position) {
                //判断位置的合法性(越界判断)
                if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
                    return false
                }
                if (this.length == 0) {
                    //空链表
                    return
                } else {
                    if (position == 0) {
                        //移除头节点
                        if (this.length == 1) {
                            this.head = null
                            this.tail = null
                        } else {
                            this.head = this.head.next
                            this.head.prev = null
                        }
                    } else if (position == this.length - 1) {
                        //移除尾结点
                        this.tail = this.tail.prev
                        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.prev = current
                    }
                    this.length--
                }
            }

 4)查找指定元素的位置

indexOf(ele) {
                let current = this.head,
                    index = 0
                while (index < this.length) {
                    if (current.data == ele) {
                        return index
                    } else {
                        current = current.next
                        index++
                    }
                }
                return -1
            }

      

 5)移除指定元素

remove(ele) {
                let index = this.indexOf(ele)
                this.removeAt(index)
            }

6)正向遍历  

forwardString() {
                let current = this.head,
                    index = 0,
                    str = "";
                while (index < this.length) {
                    str += "-" + current.data
                    current = current.next
                    index++
                }
                return str.slice(1)
            }
          

7)反向遍历 

 reverseString() {
                let current = this.tail,
                    index = this.length - 1,
                    str = "";
                while (index >= 0) {
                    str += "-" + current.data
                    current = current.prev
                    index--
                }
                return str.slice(1)
            }

8)判断是否为空

isEmpty () {
    return this.length == 0
}

9)获取链表长度

size () {
    return this.length
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈ha~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值