js算法练习--链表

class Node{
            constructor(element){
                this.element=element;
                this.next=null;
            }
        }

        

        class Linked{
            constructor(){
                this.head=null;
                this.length=0;
            }

            append(element){
                let node=new Node(element);
                if(this.head==null)
                    this.head=node;
                else{
                   
                    let current=this.head;
                    while(current && current.next){
                        current=current.next;
                    }
                    current.next=node;
                }

                this.length++;
            }

            Insert(element,position){
                if(position<0 || position>this.length)
                    return false;
                let node=new Node(element);
                let previous=null;
                let index=0;
                let current=this.head;
                for(index;index<=position;index++){
                    if(index==0 && index==position && current==null){
                        this.head=node;
                    }
                    else{
                        previous=current;
                        current=current.next;
                    }
                }
                if(index>0){
                    node.next=current;
                    previous.next=node;
                }
                this.length++;
                return true;
            }

            RemoveAt(position){
                if(position<0 || position>this.length)
                    return false;
                if(position==0){
                    if(this.head && this.head.next){
                        this.head=this.head.next;
                    }
                    else{
                        this.head=null;
                    }
                }
                else{
                    let current=this.head;
                    let previous=null;
                    let index=0;
                    while(index++<position){
                        previous=current;
                        if(current.next)
                            current=current.next;
                    }
                    if(position==this.length)
                    {
                        previous.next=null;
                    }
                    else{
                        previous.next=current.next;
                    }
                    
                }
                this.length--;
                return true;
            }
            FindElement(element){
                var index=-1;
                var current=this.head;
                while(current){
                    if(current.element===element){
                        return index+1;
                    }
                    current=current.next;
                        
                    index++;
                }
                return -1;
            }
            IsEmpty(){
                return this.length==0;
            }
            Size(){
               return this.length;
            }

            Remove(element){
                var index=this.FindElement(element);
                if(index==-1)
                    return false;
                else 
                    return this.RemoveAt(index);
            }
            ToString(){
                debugger;
                let data=[];
                let current=this.head;
                while(current){
                    data.push(current.element);
                    current=current.next;
                }
                return data.toString();
            }
        }
        let link=new Linked();
        link.append(1);

 

转载于:https://www.cnblogs.com/shuajing/p/11319043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值