7月15日

数据结构第二章的插入的C++实现

其实这道题纠结了好几天了,编程总归是需要耐心的。大二时数据结构的实验没搞明白,这次理解了

把算法变成C++,还得靠理解才行.补充一下,倒数第6和第9行的代码如果在VC中使用ASSIST插件,.不能自动变成->,需手动改

typedef int ElemType;
typedef struct
{
    ElemType *elem; //存储空间基址,即数组名
    int length;     //当前长度
    int listsize;   //当前分配的存储容量
}SqList;            //顺序表类型

int insert(SqList *S, int number, ElemType one)
{
    //在S(数组长度为number)中添加新项one
    //如果新纪录插入成功,返回1,否则返回0
    int k, j, num = 0;
    while(S->elem[num] != NULL) num++;
    if(num == number - 1) {
        cout << "已经存满,无法再存入新项!\n";
        return 0;
    }
    for(k = 0; k < num; k++)
        if(S->elem[k] >= one && S->elem[k+1] < one)
            break;
    for(j = num; j > k; j--){
        S->elem[j + 1] = S->elem[j];
    }
        S->elem[k+1] = one;
        S->length++;
        S->listsize++;
        return 1;
}

己所不欲勿施于人;练兵先练将
原文地址:https://www.cnblogs.com/ubiwind/p/2592393.html