js算法练习--双向链表

帅比萌擦擦* 提交于 2019-11-26 12:07:41
 class Node{
            constructor(element){
                this.element=element;
                this.pre=null;
                this.next=null;
            }
        }
        
        class DoubleLink{
            constructor(){
                this.head=null;
                this.tail=null;
                this.length=0;
            }

            Append(element)
            {
                var node=new Node(element);
                if(this.head==null)
                {
                    this.head=node;
                    this.tail=node;
                }
                else{
                    this.tail.next=node;
                    node.pre=this.tail;
                    this.tail=node;
                }
                this.length++;
            }
            Insert(element,position){
                if(position<0 || position>this.length)
                    return false;
                var node=new Node(element);
                //在头结点插入
                if(position==0)
                {
                    if(this.head==null)
                    {
                        this.head=node;
                        this.tail=node;
                    }
                    else{
                        node.next=this.head;
                        this.head.pre=node;
                        this.head=node;
                    }
                }
                //在中间节点插入
                if(position>0 && position<this.length){
                    var index=0;
                    var current=this.head;
                    var pre=this.head;
                    while(index++<position){
                        pre=current;
                        current=current.next;
                    }
                    node.next=current;
                    node.pre=current.pre;
                    current.pre=node;
                    pre.next=node;
                }
                //在尾节点插入
                if(position==this.length && position>0){
                    this.tail.next=node;
                    node.pre=this.tail;
                    
                    this.tail=node;
                }
                this.length++;
                return true;
                
            }

            RemoveAt(position){
                if(position<0 || position>=this.length)
                    return false;
                //头
                if(position==0){
                    this.head=this.head.next;
                    this.head.pre=null;
                    if(this.length==1){
                        this.tail=null;
                    }
                }
                //尾
                if(position==this.length-1 && position>0){
                    this.tail=this.tail.pre;
                    this.tail.next=null;
                   
                }
                //中间
                if(position>0 && position<this.length-1){
                    var index=0;
                    var current=this.head;
                    var previous=this.head;
                    while(index++<position){
                        previous=current;
                        current=current.next;
                    }
                    previous.next=current.next;
                    current.next.pre=previous;
                }
                this.length--;
                return true;
            }

        }

        var double=new DoubleLink();
        double.Append(1);

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!