用js写一个链表(详细注释)

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
//先定义节点
class linkNode {
    val;
    next;
    constructor(val, next) {
        this.val = val;
        this.next = next
    }
}

//定义链表
//链表包含头指针,尾指针,和链表长度

class linkedList {
    constructor() { //初始化一个空链表
        this._size = 0;
        this._head = null;  //头结点
        this._tail = null;  //尾节点
    }
    //在头节点前加入节点
    addAtHead(val) {
        let node = new linkNode(val, this._head)    //在头指针前加入,就算让这个节点指向头结点。 
        this._head = node       //因为实在最前面加入的,就是把加入的这个节点变成头结点
        this._size++
        if (!this._tail) {
            this._tail = node   //当通过这个方法创建第一个节点时,尾指针是空。这个元素就即时头指针也是尾指针。
        }
    }

    //在末尾加入节点
    addAtTail(val) {
        let node = new linkNode(val, null)
        if (this._tail) {           //当存在尾节点时,就可以直接改变尾指针的内容
            this._tail.next =  node(已修正)
            this._tail = node
        } else {                    //当不存在尾节点时,该表为空表,加入的这个节点即使头结点,又是尾节点
            this._tail = node
            this._head = node
        }
    }

    //在链表中的第 index 个节点之前添加值为 val 的节点。
    addAtIndex(index, val) {
        if (index > this._size) {
            return "索引不存在"
        }
        if (index == 0) {
            //当索引值时0时,复刻在头结点创建的过程
            let node = new linkNode(val, this._head)
            this._head = node
            this._size++
            if (!this._tail) {
                this._tail = node
            }
        }
        else {
            let courent = this._head
            let count = 0
            while (count < index - 1) {
                courent = courent.next
                count++
            }
            let node = new linkNode(val, courent.next)
            courent.next = node
            //如果时在链表尾部创建的话
            if (index == this._size) {
                this._tail = node
            }
            this._size++
        }

    }

    //根据索引获取节点,参数是 index 这里索引是从0开始的
    getByIndex(index) {
        if (index < 0 || index >= this._size) {
            return "索引无效"
        }
        let count = 0;
        let cur = this._head
        while (count != index) {
            cur = cur.next
            count++
        }
        return cur.val
    }

    //在删掉索引处的节点
    deleteAtIndex(index) {
        //先判断索引是否有效
        if (index < 0 || index >= this._size) {
            return "索引无效"
        }

        //删除头结点时:
        if (index == 0) {
            this._head = this._head.next
            //当链表只有一位时,即头结点和为节点相同就需要把头节点和尾节点都删掉
            if (this._size == 1) {
                this._tail = null
            }
            this._size--
        }
        //正常删除情况:
        else {
            var count = 0;
            var cur = this._head
            while (count != index - 1) {
                cur = cur.next
                count++
            }
            cur.next = cur.next.next
            //如果删除的时最后一个节点,需要改变尾节点的位置
            if (index + 1 == this._size) {
                this._tail = cur
            }
            this._size--
        }


    }
}

var listOne = new linkedList()
listOne.addAtHead(2)
listOne.addAtHead(5)
listOne.addAtHead(100)
listOne.addAtHead(5200)
listOne.addAtIndex(4, 111)

console.log(listOne)
// console.log(listOne._head.next)

// console.log(listOne.getByIndex(1))

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值