js定义链表

链表
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

// 节点类
class Node {
    // 构造函数
    constructor(val) {
        // console.log('节点类的val', ...val, val, ...[1, 2, 3])
        this.val = val;
        this.next = null
    }
}
// const node = new Node(0, 1)

// 链表类
class LinkedList {
    // 构造函数
    constructor() {
        this.head = null;//头指针
        this.length = 0;//链表长度
    }

    // 新增节点
    append(val) {
        let node = new Node(val);
        let current;//暂存当前位置
        if (this.head === null) {
            // 如果头节点为空 那么当前节点作为头节点
            this.head = node
        } else {
            current = this.head;
            while (current.next) {
                // console.log(current.next, 'current.next111111111111')
                current = current.next //遍历找到链表尾部
            }
            current.next = node
        }

        this.length++;
    }

    // 删除节点,并获得删除节点的值
    removeAt(index) {
        // 预防下标越界
        if (index > -1 && index < this.length) {
            var current = this.head;//暂存当前位置
            var previous;//暂存当前位置的前一个
            var position = 0;

            // 要删除的是第一个位置,就得改变头指针指向
            if (index === 0) {
                this.head = current.next;
            } else {
                //遍历直到current处于index位置
                //        1->2->3->4
                //index   0  1  2  3
                while (position++ < index) {
                    previous = current;
                    // 此时current在index处
                    //  previous在index-1处
                    current = current.next;
                }
                // 改变链表结构,跳过index处
                previous.next = current.next;
            }
            this.length--;
            return current.val;
        } else {
            return null
        }
    }

    // 插入节点
    insert(index, val) {
        if (index > -1 && index <= this.length) {
            let node = new Node(val);
            let current = this.head;
            let previous;
            let position = 0;
            if (index === 0) {
                node.next = current;
                this.head = node;
            } else {
                while (position++ < index) {
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;
            }
            this.length++;
            return true;//插入成功
        } else {
            return false;//插入失败
        }
    }

    // 获取索引
    indexOf(val) {
        //        1->2->3->4
        //index   0  1  2  3
        let current = this.head;
        let position = 0;
        while (current) {
            if (current.val === val) { return position }
            position++;
            current = current.next
        }
        return -1;
    }

    // 将链表转换成字符串
    toString() {
        let current = this.head;
        let string = ''
        while (current) {
            string += current.val + (current.next ? ',' : '');
            current = current.next;
        }
        return string;
    }

    // 链表长度
    size() {
        return this.length;
    }

    // 判断链表是否为空
    isEmpty() {
        return this.length === 0
    }
}

const list = new LinkedList()
// console.log(list)

list.append(1)
// console.log(list)

list.append(2)
// console.log(list)

list.append(3)
// console.log(list)

list.append(4)
// console.log(list)


//        1->2->3->4
//index   0  1  2  3
// list.removeAt(2)
// console.log(list)


// list.insert(1, 33)
// console.log(list)

// console.log(list.indexOf(4))
// console.log(list.indexOf(5))

console.log(list.toString())
console.log(list.size())
console.log(list.isEmpty())

参考博文:https://blog.csdn.net/weixin_46224014/article/details/121178538

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值