// 链表
function LinkedList () {
    function Node(value) {
        this.next = null
        this.value = value
    }
    this.head = null
    this.length = 0
    //在链表尾部追加元素
    LinkedList.prototype.add = function  (value) {
        if(!value) {
            return false
        }
        var newElement = new Node(value)
        if(!this.head) {
            this.head = newElement
        } else  {
            var current = this.head
            while(current.next) {
                current = current.next
            }
            current.next = newElement
        }
        this.length ++
        return true
    }
    //把链表元素 拼接成字符串
    LinkedList.prototype.toString = function () {
        if(!this.head) {
            return ''
        }
        var current = this.head
        var result = ''
        while(current) {
            result += current.value + ' '
            current = current.next
        }
        return result
    }
    //在链表中选择位置进行插入
    LinkedList.prototype.insertELe = function (value, position) {
          //越界判断
        if(position < 0 || position > this.length) {
            return false
        }
        var newElement = new Node(value)
        var index = 0
        var prev
        //当插入的位置为链表头的情况
        if(position == 0) {
            newElement.next = this.head
            this.head = newElement
        } else {
            var current = this.head
            while(index < position) {
                prev = current
                index ++
                current = current.next
            }
            newElement.next = current
            prev.next = newElement
        }
        this.length ++
        return true
    }
    //修改某个位置的值
    LinkedList.prototype.update = function (value, position) {
          //越界判断
        if(position < 0 || position > this.length) {
            return false
        }
        var index = 0
        var current = this.head
        while(index < position) {
            index ++
            current = current.next
        }
        current.value = value
        return true
    }
    //获取某个位置的值
    LinkedList.prototype.get = function (position) {
        //越界判断
        if(position > this.length || position < 0) {
            return false
        }
        var index = 0
        var current = this.head
        while(index < position) {
            index ++
            current = current.next
        }
        return current.value
    }
    //获取某个值得索引
    LinkedList.prototype.indexOf = function (value) {
        var current = this.head
        var index = 0
        while(current) {
            if(value == current.value) {
                return index
            }
            index ++
            current = current.next
        }
        return false
    }
    //删除某个位置的值
    LinkedList.prototype.removeAt = function (position) {
          //越界判断
          if(position > this.length || position < 0) {
            return false
        }
        var index = 0
        var current = this.head
        var prev
        while(index < position) {
            prev = current
            index++
            current = current.next
        }
        prev.next = current.next
        this.length --
        return true
    }
    //删除某个元素
    LinkedList.prototype.remove = function (element) {
       var index = this.indexOf(element)
      return this.removeAt(index)
    }
    //查看链表是否为空
    LinkedList.prototype.isEmpty = function () {
        return this.length == 0
    }
    //查看链表的长度
    LinkedList.prototype.size = function () {
        return this.length
    }
}
        // 链表
function LinkedList () {
    function Node(value) {
        this.next = null
        this.value = value
    }
    this.head = null
    this.length = 0
    //在链表尾部追加元素
    LinkedList.prototype.add = function  (value) {
        if(!value) {
            return false 
        }
        var newElement = new Node(value)
        if(!this.head) {
            this.head = newElement
        } else  {
            var current = this.head
            while(current.next) {
                current = current.next
            }
            current.next = newElement
        }
        this.length ++
        return true
    }
    //把链表元素 拼接成字符串
    LinkedList.prototype.toString = function () {
        if(!this.head) {
            return ''
        }
        var current = this.head
        var result = ''
        while(current) {
            result += current.value + ' '
            current = current.next 
        } 
        return result
    }
    //在链表中选择位置进行插入
    LinkedList.prototype.insertELe = function (valueposition) {
          //越界判断
        if(position < 0 || position > this.length) {
            return false
        }
        var newElement = new Node(value)
        var index = 0
        var prev
        //当插入的位置为链表头的情况
        if(position == 0) {
            newElement.next = this.head
            this.head = newElement
        } else {
            var current = this.head
            while(index < position) {
                prev = current
                index ++
                current = current.next
            }
            newElement.next = current
            prev.next = newElement
        }
        this.length ++
        return true
    }
    //修改某个位置的值
    LinkedList.prototype.update = function (valueposition) {
          //越界判断
        if(position < 0 || position > this.length) {
            return false
        }
        var index = 0
        var current = this.head
        while(index < position) {
            index ++
            current = current.next
        }
        current.value = value
        return true
    }
    //获取某个位置的值
    LinkedList.prototype.get = function (position) {
        //越界判断
        if(position > this.length || position < 0) {
            return false
        }
        var index = 0
        var current = this.head 
        while(index < position) {
            index ++
            current = current.next
        }
        return current.value
    }
    //获取某个值得索引
    LinkedList.prototype.indexOf = function (value) {
        var current = this.head
        var index = 0
        while(current) {
            if(value == current.value) {
                return index
            }
            index ++
            current = current.next
        }
        return false
    } 
    //删除某个位置的值
    LinkedList.prototype.removeAt = function (position) {
          //越界判断
          if(position > this.length || position < 0) {
            return false
        }
        var index = 0
        var current = this.head
        var prev
        while(index < position) {
            prev = current
            index++
            current = current.next
        }
        prev.next = current.next
        this.length --
        return true
    }
    //删除某个元素
    LinkedList.prototype.remove = function (element) {
       var index = this.indexOf(element)
      return this.removeAt(index)
    }
    //查看链表是否为空
    LinkedList.prototype.isEmpty = function () {
        return this.length == 0
    }
    //查看链表的长度
    LinkedList.prototype.size = function () {
        return this.length
    }
}
01-15 12:17