链表

前端路由

1 .

完整代码

class Node{
   constructor(value,next,pre){
        this.value=value
        this.next=null
        this.pre=null
   }
   toString(){
       console.log(this.value)
   }
}

class List{
    constructor(){
        this.head=null
        this.tail=null
        this.len=0
    }
    preAppend(value){
        const newNode=new Node(value)
        if(!this.head){
            // 没有的话就让他当头
            // 没有就不让他从前面插入,但是感觉前一个比较好

            this.head=newNode
            this.tail=newNode
            this.pre=null
            this.next=null
            return this
        }
        const yuan=this.head
        this.head=newNode
        this.head.next=yuan
        yuan.pre=this.head
        return this;
    }
    append(value){
        const newNode=new Node(value)

        if(!this.head){
            this.tail=newNode
            this.head=newNode
            this.pre=null
            this.next=null
            return this
        }
        const preNode=this.tail
        this.tail.next=newNode
        this.tail=newNode
        this.tail.pre=preNode
        return this
    }
    // 输出一个数组
    toArray(){
        if(!this.head){
            return null
        }else{
            const nodes=[]
            let currentNode=this.head
            while(currentNode){
                nodes.push(currentNode.value)
                currentNode=currentNode.next
            }
            return nodes
        }
    }
    //使用数组填充
    useArray(arr){
        [...arr].map((e)=>{
            this.append(e)
        })
    }
    // 搜索询链表中的值
    find(value){
        const findAround=this.toArray()
        // if(findAround.includes(value)){
        //     // 只是返回是否有无此数据
        //     return true
        // }else{
        //     return false
        // }

        // 返回当前的数,前一个,后一个
        if(findAround.indexOf(value)){
            const currentIndex=findAround.indexOf(value)
            const preIndex=currentIndex-1
            const nextIndex=currentIndex+1
            return {
                currentIndex:value,
                pre:findAround[preIndex],
                next:findAround[nextIndex]
            }
        }else{
            return false
        }
    }
    // 删除表中的一个值,默认是表头,传入其他的值表示从后面数第几个值
    pop(value=1){
        // 只剩一个是不能进行pop的
        const len=this.toArray().length
        let invalue=value
        if(invalue==1 && len>=2 &&len>invalue){
            const tail=this.tail
            this.tail=this.tail.pre
            this.tail.next=null
            return this
        }else if(invalue!=1 && len>=2 &&len>invalue){
            let currentIndex=this.tail
            while(invalue>1){
                currentIndex=currentIndex.pre
                invalue-- 
            }
            currentIndex.pre.next=currentIndex.next
            currentIndex.next.pre=currentIndex.pre
            
        }else{
            return false
        }
        
    }
    // 以值为单位进行删除
    popValue(value){
        const arrayAround=this.toArray()
        console.log(arrayAround.indexOf(value))
        if (arrayAround.indexOf(value)!==-1){
            this.pop((arrayAround.length)-arrayAround.indexOf(value))
        }else{
            console.log('没有这个值')
            return false;
        }
    }
    // 删除头结点
    deleteHead(){
        if(!this.head){
            console.log('已经没有了,就别再删除了')
        }

        const newHead=this.head.next
        this.head=newHead
    }

    // 当前坐标-默认的是返回tail
    currenttail(){
        if(this.tail){
            return this.tail.value
        }else{
            console.log('空数组')
        }
    }

    // tail向前移动-读取里面的值
    tailPre(){
        if(this.tail.pre){
            this.tail=this.tail.pre
            return this.tail.value
        }else{
            console.log('没有')
        }
    }

    // tail向后移动-读取里面的值
    tailNext(){
        if(this.tail.next){
            this.tail=this.tail.next
            return this.tail.value
        }else{
            console.log('没有下一个值了')
        }
    }
    // 判断是否为空的函数,或者整个list加个属性吧
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,663评论 25 708
  • 单链表 单链表问题与思路 找出单链表的倒数第K个元素(仅允许遍历一遍链表)使用指针追赶的方法。定义两个指针fast...
    wxkkkkk阅读 617评论 0 0
  • 今天的长沙下了一点小雨,碰巧一变天就感冒的我,这时候一个人捧着手机窝在出租房里无所世事。 突然很怀恋广州的天气,这...
    微艳阅读 198评论 0 3
  • 【七月海伦】 七月, 阳台飞临一支蜂鸟, 我才知道, 年已经过去一半。 海伦的夏天, 什么事都在半途中, 北门的新...
    海沐微风阅读 221评论 0 0