js实现单向链表和双向链表

//js实现链表
    //单向链表
    function LinkedList() {
        //单链表的节点结构
        function Node(ele){
            this.element = ele;
            this.next = null;
        }
        this.head = null;
        this.length = 0;
        //向链表的尾部追加一个元素
        this.append = function(ele){
            var newNode = new Node(ele),current;
            if(!this.head){
                this.head = newNode;
            }else{
                current = this.head;
                if(current.next) {
                    current = current.next;
                }
                current.next = newNode;
            }
            this.length++;
        }
        //根据元素的位置来移除某一个元素,移除成功返回元素的值
        this.removeAt = function(pos){
            if(pos>=0 && pos<this.length){
                var current = this.head,previous,index = 0;
                if(pos === 0){
                    this.head = current.next;
                }else{
                    while(index++ < pos){
                        previous = current;
                        current = current.next;
                    }
                    previous.next = current.next;
                }
                this.length--;
                return current.element;
            }else{
                return null;
            }
        }
        //在任意位置插入元素
        this.insert = function(pos,ele){
            if(pos>-1 && pos<this.length) {
                var node = new Node(ele),current = this.head,previous,index = 0;
                if(pos === 0){
                    node.next = current;
                    this.head = node;
                }else{
                    while(index++ < pos){
                        previous = current;
                        current = current.next;
                    }
                    node.next = current;
                    previous.next = node;
                }
                this.length++;
                return true;
            }else{
                return false;
            }
        }
        //输出链表
        this.toString = function(){
            var current = this.head;
            var str = '';
            while(current){
               str += current.element + (current.next ? ',':'');
               current = current.next;
            }
            return str;
        }
        //查找元素
        this.indexOf = function(ele){
            var current = this.head,index=-1;
            while(current){
                index++;
                if(current.element === ele){
                    return index;
                }else{
                    current = current.next;
                }
            }
            return -1;
        }
        //根据某一个节点的值来删除节点
        this.remove = function(ele){
            var index = this.indexOf(ele);
            return this.removeAt(index);
        }
        //判断链表是否为空
        this.isEmpty = function(){
            return this.length === 0;
        }
        //获取链表长度
        this.size = function(){
            return this.length;
        }
        //获取链表的第一个节点
        this.getHead = function () {
            return this.head;
        }
    }
    
    
    //js实现双向链表
    function DoubleLinkedList() {
        //双向链表的节点结构
        function Node(ele){
            this.element = ele;
            this.prev = null;
            this.next = null;
        }
        this.head = null;
        this.tail = null;
        this.length = 0;
        //在任意位置插入元素
        this.insert = function(pos,ele){
            if(pos > -1 && pos < this.length){
                var current = this.head,previous,index = 0,node = new Node(ele);
                if(pos === 0){
                    if(!this.head){
                        this.head = node;
                        this.tail = node;
                    }else{
                        node.next = current;
                        current.prev = node;
                        this.head = node;
                    }
                }else if(pos === this.length){
                    current = this.tail;
                    current.next = node;
                    node.prev = current;
                    this.tail = node;
                }else{
                    while(index++ < pos){
                        previous = current;
                        current = current.next;
                    }
                    node.next = current;
                    current.prev = node;
                    previous.next = node;
                    node.prev = previous;
                }
                this.length++;
                return true;
            }else{
                return false;
            }
        }
        //从任意位置移除元素
        this.removeAt = function(pos){
            if(pos>-1 && pos<this.length){
                var current = this.head,index = 0,previous;
                if(pos === 0){
                    this.head = current.next;
                    if(this.length === 1){
                        this.tail = null;
                    }else{
                        this.head.prev = null;
                    }
                }else if(pos === this.length-1){
                    current = this.tail;
                    this.tail = current.prev;
                    this.tail.next = null;
                }else{
                    while(index++ < pos){
                        previous = current;
                        current = current.next;
                    }
                    previous.next = current.next;
                    current.next.prev = previous;
                }
                this.length--;
                return current.element;
            }else{
                return null;
            }
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值