数据结构与算法五(js 实现双向链表)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双向链表</title>
</head>
<body>

<script>
    function DoublyLinkedList(){
        function Node(data){
            this.data = data;
            this.next = null;
            this.prev = null // 新添加的

        }
        this.head = null;
        this.tail = null;
        this.length = 0;

        //拼接
        DoublyLinkedList.prototype.append = function (data) {
            var newNode = new Node(data);
            if(this.head == null){
                this.head = newNode;
                this.tail = newNode;
             }else {
                var currentNode = this.head;
                while(currentNode.next){
                    currentNode = currentNode.next;
                }
                currentNode.next = newNode;
                newNode.prev = currentNode;
                this.tail = newNode;

            }
            this.length +=1;
        }
        //插入
        DoublyLinkedList.prototype.insert = function (postion,data) {
            if (postion<0 || postion> this.length) return false;

            var newNode = new Node(data);
            var currentNode = this.head;
            //1判断原来列表是否为空
            if (this.length == 0){
                this.head = newNode;
                this.tail = newNode;
            }else {
                //插入到首位
                if(postion == 0){
                    this.head.prev = newNode;
                    newNode.next = this.head;
                    this.head = newNode;
                }else if(postion == this.length)//插入到尾部
                {
                    newNode.prev = this.tail;
                    this.tail.next = newNode;
                    this.tail = newNode;

                }else {//中间插入
                    var currentNode = this.head;
                    var index = 0;
                    while (index++ <postion){
                        currentNode = currentNode.next;
                    }
                    newNode.prev = currentNode.prev;
                    newNode.next = currentNode;
                    currentNode.prev.next = newNode;
                    currentNode.prev = newNode;
                }
                this.length +=1;
                return true;

            }
        }
        //获取根据位置
        DoublyLinkedList.prototype.get = function (position) {
            if (position < 0 || position >= this.length) return null;
            var currentNode = position *2 < this.length ? this.head : this.tail;
            var index = 0;
            if (position *2 < this.length){//正向查询
//                currentNode =  this.head;
                while (position> index++){
                    currentNode = currentNode.next;
                }
            }else {//反向查询
//                currentNode = this.tail;
                index = this.length -1;
                while (position < index--){
                    currentNode = currentNode.prev;
                }
            }
            return currentNode.data;
        }
        //根据位置获取元素
        DoublyLinkedList.prototype.indexOf = function (data) {
            var currentNode = this.head;
            var index = 0;
            while (currentNode){
                if(currentNode.data == data) return index;
                else {
                    currentNode = currentNode.next;
                    index+=1;
                }
            }
            return -1;
        }
        //更新数据根据位置和数据
        DoublyLinkedList.prototype.update = function (position,data) {
            if (position < 0 || position >= this.length) return false;
            var currentNode = position *2 < this.length ? this.head : this.tail;
            var index = 0;
            if (position *2 < this.length){//正向查询
                while (position> index++){
                    currentNode = currentNode.next;
                }
            }else {//反向查询
                index = this.length -1;
                while (position < index--){
                    currentNode = currentNode.prev;
                }
            }
            currentNode.data = data;
            return true;
        }
        //根据位置移除
        DoublyLinkedList.prototype.removeAt = function (position) {
            if (position < 0 || position >= this.length) return false;

            if(this.length == 1){
                this.head = null;
                this.tail = null;
            }else {
                if (position == 0){
                    this.head = this.head.next;
                    this.head.prefix = null;
                }else if(position == this.length -1){
                    this.tail = this.tail.prev;
                    this.tail.next = null;
                }else {
                    var currentNode = position *2 < this.length ? this.head : this.tail;
                    var index = 0;
                    if (position *2 < this.length){//正向查询
                        while (position> index++){
                            currentNode = currentNode.next;
                        }
                    }else {//反向查询
                        index = this.length -1;
                        while (position < index--){
                            currentNode = currentNode.prev;
                        }
                    }
                    currentNode.prev.next = currentNode.next;
                    currentNode.next.prev = currentNode.prev;
                }
            }
            return true;
        }
        //移除某元素
        DoublyLinkedList.prototype.remove = function (data) {
            var index = this.indexOf(data);
            if(index == -1) return false;
            return this.removeAt(index);
        }
        //是否为空
        DoublyLinkedList.prototype.isEmpty = function () {
            return this.length == 0;
        }
        //有多少元素
        DoublyLinkedList.prototype.size = function () {
            return this.length;
        }
        //元素内容
        DoublyLinkedList.prototype.toString = function () {
            return this.backwardString();
        }
        //
        DoublyLinkedList.prototype.fowardString = 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;
        }


    }
    var dLink = new DoublyLinkedList();
    dLink.append('a');
    dLink.append('b');
    dLink.append('c');
    dLink.append('d');
    dLink.append('e');

//    dLink.insert(0,'0');
//    alert(dLink);
//    dLink.insert(6,'F');
//    alert(dLink);
//    dLink.insert(5,'5');
//    alert(dLink);
//       alert(dLink.backwardString());
//    alert(dLink.fowardString());
//    alert(dLink.get(3));
//    alert(dLink.indxOf('f'))
//    dLink.update(4,'D');
//    dLink.removeAt(4);
    dLink.remove('m');
    alert(dLink);
</script>

</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值