JS 双向链表的封装和引用

<!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 Node(data) {
                this.data = data;
                this.next = null;
                this.prev = null;
            }
            DoublyLinkedList.prototype.append = function(data) {
                    //1.创建data新节点
                    var newNode = new Node(data);
                    //2.判断添加的是否为第一个节点
                    if (this.length == 0) {
                        this.head = newNode
                        this.tail = newNode
                    } else {
                        newNode.prev = this.tail;
                        this.tail.next = newNode
                        this.tail = newNode
                    }
                    this.length += 1
                }
                //2.toString()
            DoublyLinkedList.prototype.toString = function() {
                return this.backWardString()
            }
            DoublyLinkedList.prototype.forWardString = function() {
                var current = this.tail;
                var resultString = "";
                while (current) {
                    resultString += current.data + " ";
                    current = current.prev

                }
                return resultString;
            }


            DoublyLinkedList.prototype.backWardString = function() {
                var current = this.head;
                var resultString = "";
                while (current) {
                    resultString += current.data + " ";
                    current = current.next

                }
                return resultString;
            }
            DoublyLinkedList.prototype.insert = function(position, data) {

                //越界判断
                if (position < 0 || position > this.length) {
                    return false
                }
                //创建data数据节点
                var newNode = new Node(data)
                    //第一个节点位置时
                if (this.length == 0) {
                    this.head = newNode
                    this.tail = newNode
                } else {
                    if (position == 0) {
                        this.head.prev = newNode
                        newNode.next = this.head;
                        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
                        }
                        newNode.next = current;
                        newNode.prev = current.prev
                        current.prev.next = newNode;
                        current.prev = newNode

                    }


                }
                this.length += 1;
                return true

            }
            DoublyLinkedList.prototype.get = function(position) {
                if (position < 0 || position > this.length) {
                    return null
                }
                var current = this.head;
                var index = 0;
                while (index++ < position) {
                    current = current.next;

                }
                return current.data;
            }
            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 -1;
            }
            DoublyLinkedList.prototype.updata = function(position, newData) {
                if (position < 0 || position > this.length) {
                    return false
                }
                var current = this.head;
                var index = 0;
                while (index++ < position) {
                    current = current.next;
                }
                current.data = newData;
                return true;

            }
            DoublyLinkedList.prototype.removeAt = function(position) {
                if (position < 0 || position >= this.length) {
                    return null
                }
                console.log(this.length)
                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) {
                        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;
            }
            DoublyLinkedList.prototype.remove = function(data) {
                var index = this.indexOf(data);
                return this.removeAt(index)
            }
            DoublyLinkedList.prototype.isEmpty = function() {
                return this.length == 0
            }
            DoublyLinkedList.prototype.size = function() {
                    return this.length
                }
                //获取第一个元素
            DoublyLinkedList.prototype.getFirst = function() {
                    return this.head.data
                }
                //获取最后一个元素
            DoublyLinkedList.prototype.getLast = function() {
                return this.tail.data
            }

        }

        var list = new DoublyLinkedList();
        list.append("abc")
        list.append("cba")
        list.append("nba")

        // console.log(list.forWardString())
        console.log("-------------------------------------------------------")
        list.insert(0, 'aaa');
        list.insert(4, 'bbb');
        list.insert(2, 'ccc');
        console.log(list.backWardString())
        console.log(list.indexOf('aaa'))
        console.log(list.indexOf('cba'))
        console.log(list.indexOf('aa'))
        list.updata(0, 'mmm')
        list.updata(1, 'mmm')
        list.updata(2, 'mmm')
        console.log(list.backWardString())
            //ist.removeAt(0)
        console.log(list.backWardString())
            //list.removeAt(1)
        list.remove("nba")
        console.log(list.getFirst())
        console.log(list.getLast())
    </script>
</body>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

优价实习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值