javascript——双向链表

双向链表:既可从头遍历到尾又可从尾遍历到头。

封装双向链表代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function DoublyLinkedList() {
            // 属性
            this.head = null;
            this.tail = null;
            this.length = 0;

            //内部类:节点类
            function Nodes(data) {
                this.prev = null;
                this.next = null;
                this.data = data;
            }

            //方法
            // 1.向后新增节点
            DoublyLinkedList.prototype.append = function (data) {
                //创建新节点
                var newNode = new Nodes(data);

                //判断是否为第一个
                if (this.head == null) {
                    this.head = newNode;
                    this.tail = newNode;
                } else {
                    this.tail.next = newNode;
                    newNode.prev = this.tail;
                    this.tail = newNode;
                }

                this.length++
            }

            // 2.将链表转化为字符串
            // 2.1 toString方法
            DoublyLinkedList.prototype.toString = function () {
                return this.backwordString()
            }

            // 2.2 forwordString方法
            DoublyLinkedList.prototype.forwordString = function () {
                var current = this.tail
                var str = ""

                while (current) {
                    str += current.data + "\xa0\xa0\xa0"
                    current = current.prev
                }

                return str
            }

            // 2.3 backwordString方法
            DoublyLinkedList.prototype.backwordString = function () {
                var current = this.head
                var str = ""

                while (current) {
                    str += current.data + "\xa0\xa0\xa0"
                    current = current.next
                }

                return str
            }


            //3.insert方法:向链表特定位置插入一个新的节点
            DoublyLinkedList.prototype.insert = function (position, data) {
                //越界判断
                if (position < 0 || position >= this.length) return false

                //根据data创建新的节点
                var newNode = new Nodes(data)

                // 判断原来的列表是否为空
                if (this.length == 0) {
                    this.head = newNode
                    this.tail = newNode
                } else {
                    if (position == 0) {  //在列表头部差入
                        newNode.next = this.head
                        this.head.prev = newNode
                        this.head = newNode
                    } else if (position == this.length) { //在列表尾部差入
                        newNode.prev = this.tail
                        this.tail.next = newNode
                        this.tail = newNode
                    } else {  //在列表中间插入
                        var current = this.head
                        var index = 0
                        while (index++ < position) {
                            current = current.next
                        }

                        current.prev.next = newNode
                        newNode.prev = current.prev
                        current.prev = newNode
                        newNode.next = current
                    }
                    this.length++
                    return true
                }
            }

            // 4.get方法
            DoublyLinkedList.prototype.get = function (position) {
                //越界判断
                if (position < 0 || position >= this.length) return null

                //如果position小于等于this.length / 2从前往后查找,如果position大于等于this.length / 2从后往前查找
                if (position <= this.length / 2) {
                    var current = this.head
                    var index = 0
                    while (index++ < position) {
                        current = current.next
                    }
                } else {
                    var current = this.tail
                    var index = this.length - 1
                    while (index-- > position) {
                        current = current.prev
                    }
                }
                return current.data
            }

            // 5.indexOf方法
            DoublyLinkedList.prototype.indexOf = function (data) {
                var current = this.head
                var index = 0
                while (current) {
                    if (current.data == data) return index
                    current = current.next
                    index++
                }
                return false
            }

            //6.update方法
            DoublyLinkedList.prototype.update = function (position, data) {
                if (position < 0 || position >= this.length) return false

                if (position <= this.length / 2) {
                    var current = this.head
                    var index = 0
                    while (index++ < position) {
                        current = current.next
                    }
                }else{
                    var current = this.tail
                    var index = this.length - 1
                    while (index-- < position) {
                        current = current.prev
                    }
                }
                current.data = data
                return true
            }


            //7.removeAt方法:根据position删除元素
            DoublyLinkedList.prototype.removeAt = function(position){
                if (position < 0 || position >= this.length) return null

                var current = this.head

                //判断是否只有一个节点
                if(this.length == 1){
                    this.head = null 
                    this.tail = null
                }else{
                    if(position == 0){ //删除的是第一个节点
                        this.head.next.prev = null
                        this.head = this.head.next
                    }else if(position == this.length - 1){ //删除的是最后一个节点
                        current = this.tail
                        this.tail.prev.next = null
                        this.tail = this.tail.prev
                    }else{
                        var index = 0
                        while(index++ < position){
                            current = current.next
                        }
                        current.prev.next = current.next
                        current.next.prev = current.prev
                    }
                }
                this.length --
                return current.data
            }


            //8.remove方法:根据data删除元素
            DoublyLinkedList.prototype.remove = function (data){
                var position = this.indexOf(data)
                return this.removeAt(position)
            }

            //9.isEmpty方法
            DoublyLinkedList.prototype.isEmpty = function (){
                return this.length == 0
            }

            //10.size方法
            DoublyLinkedList.prototype.size = function (){
                return this.length
            }
        }

        var list = new DoublyLinkedList()
        list.append("aaa")
        list.append("bbb")
        list.append("ccc")
        list.insert(2, "ddd")
        alert(list)
        list.update(3, "000")
        list.remove("aaa")
        alert(list.isEmpty())
    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值