码迷,mamicode.com
首页 > 其他好文 > 详细

链表封装

时间:2019-10-03 12:45:44      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:ati   prototype   情况   index   ext   选择   als   位置   链表   

// 链表
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
    }
}

链表封装

标签:ati   prototype   情况   index   ext   选择   als   位置   链表   

原文地址:https://www.cnblogs.com/CoderZX/p/11619344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!