数据结构-JavaScript实现线性顺序表


{
    class SequenceList {
        length: number;
        maxLength: number;
        data: any[];
        constructor(maxLength: number) {
            this.data = [];
            this.maxLength = maxLength;
            this.length = 0;
        }
        // 尾部插入
        push(element: any) {
            this.data[this.length] = element;
            this.length++;
        }
        // 在指定位置插入
        insertForIndex(index: number, element: any) {
            // 判断是否超过最大数量
            if (this.length >= this.maxLength) {
                throw new Error('Insert the failure, cannot exceed the max length!');
            }
            // 判断是否指定位置是否合法
            if (typeof (index) !== 'number' || index < 0 || index > this.maxLength - 1) {
                throw new Error('Insert the failure, index error! ');
            }
            // 从index开始,后面的元素后移一位
            for (let i = this.length; i > index; i--) {
                this.data[i] = this.data[i - 1];
            }
            // 赋值
            this.data[index] = element;
            this.length++;
        }
        // 删除指定元素
        removeForElement(element: any) {
            const index = this.getElementIndex(element);
            if (index < 0) {
                console.error('The element was not found');
                return;
            }
            for (let i = index; i < this.length - 1; i++) {
                this.data[i] = this.data[i + 1];
            }
            this.data.length = this.length - 1;
            this.length--;
        }
        // 获取元素所在位置
        getElementIndex(element: any) {
            let index = -1;
            for (let i = 0; i < this.length; i++) {
                if (element === this.data[i]) {
                    index = i;
                    break;
                }
            }
            return index;
        }
        print() {
            for (let i = 0; i < this.length; i++) {
                console.log(this.data[i]);
            }
        }
    }
    const a = new SequenceList(5);
    a.push(1);
    a.push(2);
    a.push(3);
    a.push(4);
    a.push(5);
    console.log('指定元素位置 :', a.getElementIndex(3));
    a.removeForElement(2);
    console.log('当前长度 :', a.length);
    a.insertForIndex(4, 6)
    a.print();
    console.log('当前长度 :', a.length);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值