JavaScript实现单链表的封装

function LinkedList() {
    //单链表的切入点就是他的头指针
    //封装结点类
    function Node(data) {
        this.data = data;
        this.next = null;
    }
    //头指针
    this.head = null;
    //链表的长度
    this.lenght = 0;
    LinkedList.prototype.apped = function (data) {
        //每次调用的时候都先创建一个节点
        let newNode = new Node(data)
        //判断是否为第一个节点
        if (this.lenght === 0) {
            this.head = newNode
        }
        //当不是第一个节点的时候
        else {
            //现在this.head指向的是第一个节点  其实就是this.head就是第一个节点
            let curr = this.head
            while (curr.next) {
                //当下一个节点是null的时候这个循环时进不来的
                //这个时候第28行的代码会将curr.next链接一个新的节点
                //再次进入循环时下一个节点将不再是null可以进入循环
                //然后将curr指向下一个节点
                curr = curr.next
            }

            curr.next = newNode
        }
        this.lenght++
    }
    //toString方法
    LinkedList.prototype.toString = function () {
        let curred = this.head
        let result = curred.data + ''
        while (curred.next) {
            curred = curred.next
            result = result + ',' + curred.data
        }
        return result
    }
    //插入
    LinkedList.prototype.insert = function (position, element) {
        let newNode = new Node(element)
        if (position < 0 || position > this.lenght) return false
        //this.head是切入点他代表的是第一个节点
        let curred = this.head
        let index = 0
        let prev = null;
        while (index++ < position) {
            prev = curred
            curred = curred.next
        }
        prev.next = newNode
        newNode.next = curred
        this.lenght++
        return true
    }
    //获取对应位置的元素
    LinkedList.prototype.get = function (position) {
        if (position < 0 || position > this.lenght) return null
        let curr = this.head
        let index = 0
        while (index++ < position) {
            curr = curr.next
        }
        return curr.data
    }
    //获取元素的对应位置
    LinkedList.prototype.indexOf = function (element) {
        let curred = this.head
        let index = 0
        while (index < this.lenght) {
            if (element === curred.data) {
                return index
            }
            curred = curred.next
            index++
        }
        return -1
    }
    //修改某个位置的元素
    LinkedList.prototype.update = function (position, element) {
        let newNode = new Node(element)
        if (position < 0 || position >= this.lenght) return null
        let index = 0;
        let curred = this.head
        //第一次进去的时候index是1但是和position比较的是0
        while (index++ < position) {
            curred = curred.next
        }
        curred.data = newNode.data
        return true
    }
    //移除特定位置的一项
    LinkedList.prototype.removeAt = function (position) {
        if (position < 0 || position >= this.lenght) return null
        let curr = this.head;
        if (position === 0) {
            this.head = curr.next
        } else {
            let index = 0;
            let previous = null;
            while (index++ < position) {
                previous = curr
                curr = curr.next
            }
            previous.next = curr.next
            this.lenght--
            return curr.data
        }
    }
    //移除列表中的某一项
    LinkedList.prototype.remove = function (element) {
        let curred = this.head
        let previous = null
        let isNull = false
        let index = 0
        while (curred) {
            index++
            if (curred.data === element) {
                break;
            } else if (index === this.lenght) {
                isNull = true
            }
            previous = curred
            curred = curred.next
        }
        if (isNull) return false
        previous.next = curred.next
        this.lenght--
        return true
    }
    //判空
    LinkedList.prototype.isEmpty = function () {
        if (this.lenght === 0) return true
        return false
    }
    //链表长度
    LinkedList.prototype.size = function () {
        return this.lenght
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值