链表封装

Posted coderzx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表封装相关的知识,希望对你有一定的参考价值。

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

以上是关于链表封装的主要内容,如果未能解决你的问题,请参考以下文章

C++封装链式表-链表

c++封装链表实现-->学生信息管理分析系统

js封装一个单链表

JavaScript数据结构之链表

JavaScript数据结构之链表

JavaScript数据结构与算法 单向链表