<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>

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

        class LinkList {
            
            constructor(){
                this.count = 0
                this.head = null
            }

           
            push(element){
                const node = new Node(element) 
             
                if(this.head === null) {
                    this.head = node  
                }else {
                    let current = this.head
                    while(current.next != null){
                        current = current.next  
                    }
                    current.next = node 
                }
                this.count++
            }

            // 指定位置删除
            removeAt(index){

                if(index >= 0 && index < this.count){
                    let current = this.head 
                    if(index === 0)  {
                        this.head = this.head.next
                    } else {
                       let previous  
                        for(let i = 0; i < index; i++){     
                            previous = current
                            current = current.next
                        }
                        previous.next = current.next
                    } 
                    this.count--
                    return current.element
                } 
            }

            getNodeAt(index) {
                if(index >= 0 && index < this.count){
                    let node = this.head
                    for(let i = 0; i <index;i++){
                        node = node.next
                    }
                    return node
                } 
                return 
            }

            // 指定位置删除
            removeAt2(index){

                if(index >= 0 && index < this.count){
                    let current = this.head 
                    if(index === 0)  {
                        this.head = this.head.next
                    } else {
                        let previous = this.getNodeAt(index - 1) 
                        current = previous.next
                        previous.next = current.next
                    } 
                    this.count--
                    return current.element
                } 
            } 

            equalFn(a,b){
                // return a === b
                return JSON.stringify(a) === JSON.stringify(b) 
                // imumutable 判断两个元素是否相等
            }

            //  返回指定数据索引
            indexOf(element){
                let current = this.head
                for(let i = 0; i < this.count;i++){
                    if(this.equalFn(element,current.element)){
                        return i
                    }
                    current = current.next
                }
                return -1
            }

            // 指定数据删除
            remove(element){
                const index = this.indexOf(element)
                return this.removeAt(index)
            }
 
            //指定位置插入一个元素
            insert(element,index){
              
                if(index >= 0 && index <= this.count){
                    const node = new Node(element) 
                    if(index === 0){
                        const current = this.head 
                        node.next = current
                        this.head = node
                    } else{
                        // 找到指定位置数据  和 上级的数据 
                        let previous = this.getNodeAt(index - 1) 
                        const current = previous.next 
                        node.next = current
                        prevent.next = node
                    } 
                    this.count++
                    return  true
                 }
                 return false

            }
        }
        
    </script>

    <script>
 
        class  DoubleNode extends Node { 
            constructor(element){
                super(element)
                this.prev = null
            }
        }

        class DoubleLinkedList extends LinkList {
            constructor(){
                super()
                this.tail = null
            }

            push(element){
                const node = new DoubleNode(element)
                if(this.head === null) {
                    this.head = node
                    this.tail = node
                } else {
                    this.tail.next = node
                    node.prev = this.tail
                    this.tail = node
                }
                this.count++
            }

            insert(element,index){
                if(index >=0 && index < this.count){
                    const node = new DoubleNode(element)
                    let current = this.head
                    if(index === 0) {
                        if(this.head === null){
                            this.head = node  
                            this.tail = node
                        } else {
                            node.next = this.head
                            this.head.prev = node   
                            this.head = node  
                        } 
                    } else if(index === this.count) {
                        current = this.tail
                        current.next = node
                        node.prev = current
                        this.tail = node
                    } else {
                        const previous = this.getNodeAt(index-1)
                        current = previous.next

                        node.next = current 
                        current.prev = node 
                        previous.next = node
                        node.prev = previous
                    }
                    this.count++  
                    return true
                }
                return false
            }

            removeAt(index) {
                if(index >= 0 && index < this.count) {
                    let current = this.head
                    if(index === 0){
                        this.head = this.head.next
                        if(this.count === 1) {
                            this.tail= null
                        } else {
                            this.head.prev = null 
                        }
                    } else if(index === this.count-1){
                        current = this.tail
                        this.tail = current.prev
                        this.tail.next = null
                    } else {
                        let previous = this.getNodeAt(index-1)
                        current = previous.next 
                        previous.next = current.next
                        currrent.next.prev = previous
                    }

                    this.count--
                    return current.element
                }
                return  
            }

            getHead(){
                return this.head
            }

            getTail(){
                return this.tail
            }

        }

        var list = new DoubleLinkedList()
        list.push(100)
        list.push(200)
        list.push(300)
        list.push(400)

        console.log(list)

    </script>
    
</body>
</html>

我是Eric