【数据结构连载一线性表】【顺序表】javascript

function List(maxSize) {
    this.maxSize = maxSize;
    this.length = 0;
    this.data = new Array(maxSize);
    this.empty = function () {
        // 顺序表是否为空
        return this.length === 0;
    };
    this.full = function () {
        // 顺序表是否已满
        return this.length === this.maxSize;
    };
    this.clear = function () {
        // 清空顺序表
        this.data = [];
        this.length = 0;
    };
    this.get = function (idx) {
        // 根据下标获取顺序表数据
        if (idx < 0 || idx > this.length) {
            throw new Error("数组角标越界");
        }
        return this.data[idx];
    };
    this.insert = function (idx, value) {
        // 顺序表插入操作
        if (this.full() === true) {
            throw new Error("顺序表已满");
        }
        if (idx < 0) {
            throw new Error("数组角标越界");
        }

        if (idx < this.length) {
            for (let i = this.length; i > idx; i--) {
                this.data[i] = this.data[i - 1];
            }
        } else {
            idx = this.length;
        }
        this.data[idx] = value;
        this.length++;
    };
    this.delete = function (idx) {
        // 顺序表删除操作
        if (idx < 0 || idx >= this.length) {
            throw new Error("数组角标越界");
        }
        for (let i = idx; i < this.length; i++) {
            this.data[i] = this.data[i + 1];
        }
        this.length--;
    };
    this.exist = function (value) {
        // 顺序表判断是否存在
        for (let i = 0; i < this.length; i++) {
            if (this.data[i] === value) {
                return true;
            }
        }
        return false;
    };
    this.add = function (value) {
        // 顺序表从头添加
        if (this.full() === true) {
            throw new Error("顺序表已满");
        }
        for (let i = this.length; i > 0; i--) {
            this.data[i] = this.data[i - 1];
        }
        this.data[0] = value;
        this.length++;
    };
    this.append = function (value) {
        // 顺序表从尾部添加
        if (this.full() === true) {
            throw new Error("顺序表已满");
        }
        this.data[this.length] = value;
        this.length++;
    };
    this.index = function (value) {
        // 获取下标
        for (let i = 0; i < this.length; i++) {
            if (this.data[i] === value) {
                return i;
            }
        }
        return -1;
    };
}


var l = new List(10);
l.insert(0, 1)
l.insert(0, 2)
l.insert(0, 3)
l.insert(0, 4)
l.delete(0)
l.delete(4)
l.add(1)
l.append(10)
console.log(l.exist(4))
console.log(l.full());
console.log(l.length);
console.log(l.data)
console.log(l.index(1))
console.log(l.get(1))
l.clear()
console.log(l.empty())

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辛勤的搬砖者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值