线性表的顺序存储结构-插入元素
//插入元素
public bool Insert(int index, T element) {
    if (index < 1 || index > this._currentLength) {
        Console.WriteLine("索引非法!");
        return false;
    }

    if (this._currentLength < this._maxLength) {
        for (int i = this._currentLength-1; i >= index-1; i--) {
            this._data[i + 1] = this._data[i];
        }

        this._data[index - 1] = element;
        this._currentLength++;
        return true;
    }

    Console.WriteLine("不能向已满的表插入元素!");
    return false;
}

线性表的顺序储存结构

先检查插入位置是否合法,总不能一下子插入到数据域的末尾孤零零一个数据吧,这样数据就不连续了

然后检查数据有没有满,满了你还插啊,插哪里去啊

最后就是将插入位置空出来,插入位置后面的元素往后挪,挪完后坐下就好了,当然长度要加1了


首页 我的博客
粤ICP备17103704号