JavaScript数据结构单链表的封装

JavaScript数据结构单链表的封装

// 封装链表
function LiskedList(){
    
    
    // 封装链表
    function Nodes(data){
    
    
        this.data = data;
        this.next = null;
    }
    // 属性
    this.header = null;
    this.length = 0;
    // append(element)  向列表尾部添加一个新的项
    LiskedList.prototype.append = function(data) {
    
    
        var newNode = new Nodes(data);
        // 1.判断是否为第一个节点
        if(this.length == 0){
    
    
            this.header = newNode
        }else{
    
    
            var currentNode = this.header
            while(currentNode.next){
    
    
                currentNode = currentNode.next
            }
            currentNode.next = newNode
        }
        this.length +=1
    }
    // insert(position,data) 向列表的特定位置插入一个新的项  在position前插入一个数据
    LiskedList.prototype.insert = function(position,data) {
    
    
        if(position<0 || position>this.length) return false
        var newNode = new Nodes(data)
        if(this.length = 0){
    
    
            newNode.next = this.header
            this.header = newNode
        }else{
    
    
            var prevNode = null
            var index = 0
            var currentNode = this.header
            while (index++<position) {
    
    
                prevNode =currentNode
                currentNode = currentNode.next
            }
            newNode.next = currentNode
            prevNode.next = newNode
        }
        this.length +=1
        return true
    }
    

    // get(position) 获取对应位置的值
    LiskedList.prototype.get = function(position){
    
    
        if(position<0 || position>=this.length){
    
    

        }
        index = 0;
        currentNode = this.header
        while(index++ < position){
    
    
            currentNode = currentNode.next
        }
        return currentNode.data

    }

    // indexOf(element) 返回元素在列表中的索引  如果列表中没有该元素返回-1
    LiskedList.prototype.indexOf = function (element) {
    
    
        index = 0
        currentNode = this.header
        while (index < this.length){
    
    
            if (currentNode.data == element) {
    
    
                return index
            }else{
    
    
                currentNode = currentNode.next
                index++
            }
        }
        return -1
    }
    
    // update(position) 修改某个位置的元素
    LiskedList.prototype.update = function(position,data){
    
    
        if(position<0||position>=this.length){
    
    
            return false
        }
        index =0
        currentNode = this.header
        while(index<position){
    
    
            currentNode = currentNode.next
            index++;
        }
        currentNode.data = data
        return true
    }
    // removeAt(position) 从列表中移除一项 根据索引
    LiskedList.prototype.removeAt = function(position){
    
    
        if(position<0 || position>= this.length){
    
    
            
            return false
        }
        if (position==0) {
    
    
            this.header = this.header.next
        }else{
    
    
            index = 0
            prevNode = null;
            currentNode = this.header
            while(index++<position){
    
    
                prevNode = currentNode
                currentNode = currentNode.next
            }
            prevNode.next = currentNode.next
        }
        this.length-=1
        return true

    }
    // remove(data)  根据数据删除某个元素
    LiskedList.prototype.remove = function(data){
    
    
        index = 0
        prevNode = null
        currentNode = this.header
        while (index <this.length){
    
    
            if(currentNode.data == data){
    
    
                if(index == 0){
    
    
                    this.header = currentNode.next
                    return true
                }else{
    
    
                    prevNode.next = currentNode.next
                    return true
                }
                
            }else{
    
    
                prevNode = currentNode
                currentNode= currentNode.next
            }
        }
        return false
    }

    
    // isEmpty() 如果链表中不包含任何元素,返回true  如果链表的长度大于0,则返回false
    LiskedList.prototype.isEmpty = function(){
    
    
        return this.length == 0
    }
    // size() 返回链表中的个数,与数组的length属性相似
    LiskedList.prototype.size = function(){
    
    
        return this.length
    }
    // toString()  由于列表项使用了node类  就需要继承自java对象默认的toString()方法,让其只输出元素的值
    LiskedList.prototype.toString = function(){
    
    
        var currentNode = this.header
        var listString = ''
        while (currentNode){
    
    
            listString += currentNode.data+" "
            currentNode = currentNode.next
        }
        return listString
    }
}
var list = new LiskedList()
list.append('l')
list.append('i')
list.append('s')
list.append('t')
list.update(2,'ss')
list.remove('l')
console.log(list.toString());

猜你喜欢

转载自blog.csdn.net/weixin_45822938/article/details/123295396