js封装一个单链表

// 封装链表
function LinkedList(){
    // 创建一个內部类,用于存节点的数据和指针
    function Node(data){
        this.data = data;
        this.next = null;
    }
    // 链表头
    this.head = null;
    // 链表长度
    this.length = 0;
    // ---------------------方法------------------

    // append 向链表结尾追加一个节点
    LinkedList.prototype.append = function(data){
        var newNode = new Node(data);
        if(!this.length){
            this.head = newNode;
        }else{
            var current = this.head;
            while(current.next){
                current = current.next
            }
            current.next = newNode
        }
        this.length += 1
    }
    // toString
    LinkedList.prototype.toString = function(){
        var current = this.head;
        var str = '';
        while(current){
            str += current.data + ' ';
            current = current.next; 
        }
        return str
    }
    // insert  向链表中插入一个节点
    LinkedList.prototype.insert = function(position,data){
        // 位置越界处理
        if(position < 0 || position > this.length) return false;
        // 创建新节点
        var newNode = new Node(data);
        // 插入头部
        if(position == 0){
            newNode.next = this.head;
            this.head = newNode;
            return true
        }
        // 插入其它位置
        let index = 0;
        let current = this.head;
        let previous = null;
        while(index++ < position){
            previous = current;
            current = previous.next; 
        }
        previous.next = newNode;
        newNode.next = current;
        this.length++;
        return true


    }
    // get    获取某个节点的信息
    LinkedList.prototype.get = function(position){
        // 越界处理
        if(position < 0 || position >= this.length) return null;
        // 获取
        let index = 0;
        let current = this.head;
        while(index++ < position){
            current = current.next;
        }
        return current.data 
    }
    // indexOf   通过元素返回对应的索引,没有的话返回-1
    LinkedList.prototype.indexOf = function(data){
        if(!this.length) return -1;
        let index = 0;
        let current = this.head;
        while(current){
            if(current.data === data){
                return index
            }
            current = current.next;
            index++;
        }
        return -1
    }
    // update   修改某个节点的信息
    LinkedList.prototype.update = function(position,newData){
        // 位置越界处理
        if(position < 0 || position >= this.length) return false;
        // 修改头部节点
        if(position == 0){
            this.head.data = newData;
            return true
        }
        // 修改其它节点
        let current = this.head;
        let index = 0;
        while(index++ < position){
            current = current.next
        }
        current.data = newData;
        return true

    }
    // removeAt   通过索引删除相应的节点
    LinkedList.prototype.removeAt = function(position){
        // 位置越界处理
        if(position < 0 || position >= this.length) return null;
        let current = this.head;
        // 删除头部节点
        if(position == 0){
            this.head = this.head.next;
            return current.data
        }
        // 删除其它节点
        let previous = null;
        let index = 0;
        while(index++ < position){
            previous = current;
            current = previous.next;
        }
        previous.next = current.next;
        this.length--;
        return current.data
    }
    // remove   通过元素删除相应的节点
    LinkedList.prototype.remove = function(data){
        // 头部节点删除
        if(this.head.data === data){
            this.head = this.head.next;
            return true
        }
        // 删除其它节点
        let current = this.head;
        let previous = null;
        while(current){
            previous = current;
            current = previous.next;
            if(current && current.data === data){
                previous.next = current.next;
                this.length--;
                return true
            }
        }
        return false
    }
    // isEmpty  判断链表是否为空
    LinkedList.prototype.isEmpty = function(){
        return this.length == 0
    }
    // size     查看链表的长度
    LinkedList.prototype.size = function(){
        return this.length
    }
}
// 测试
let list = new LinkedList();
list.append('caoCao')
list.append('liuBei')
list.append('zhangFei')
console.log(list);
console.log(list.toString());
list.insert(2,'hei')
console.log(list);
console.log(list.toString());
console.log(list.get(2));
console.log(list.indexOf('hei'));
console.log(list.update(3,'美女'));
console.log(list.toString());
console.log(list.removeAt(2));
console.log(list.toString());
console.log(list.remove('美女'));
console.log(list.toString());
console.log(list.isEmpty());
console.log(list.size());

 

posted @ 2021-06-01 14:49  正经的流刺源  阅读(131)  评论(0编辑  收藏  举报