串的顺序存储结构-指定位置插入字符数组
//插入字符数组
public bool Insert(int index , char[] chars)
{
    if (this.isEmpty()||chars == null) {
        Console.WriteLine("插入的字符不能为空!");
        return false;
    }

    int length = _data.Length;
    if (index > length || index < 1) {
        Console.WriteLine("索引位置错误!");
        return false;
    }

    int cLength = chars.Length;

    char[] term = _data;
    _data = new char[length + cLength];
    int i = 0;
    for (; i < index - 1; i++) {
        _data[i] = term[i];
    }

    for (int j = 0; j < cLength; j++) {
        _data[i] = chars[j];
        i++;
    }

    for (int k = index - 1; k < length; k++) {
        _data[i] = term[k];
        i++;
    }

    return true;
}


①先判断本串和字符数组是否为空

②判断索引的合法性

③临时保存本串的数据

④重构本串的数据域大小为原串长度+插入字符数组长度

⑤原串index前的字符,插入的字符,原串index和其后的字符,分为三段分别按顺序赋值给新的数据域

串的顺序存储结构和KMP匹配算法


首页 我的博客
粤ICP备17103704号