10.21

本题要求实现六个函数,顺序表为整型数据,可实现输入、输出、取值、查找、插入、删除功能。输入样例与输出样例对应情况见下图。

无标题.jpg

函数接口定义:
顺序表描述的结构体为
typedef struct {
ElemType *elem; //存储空间的基地址
int length; //当前长度
} SqList;

需要实现函数的接口分别为:

int GetElem(SqList L, int i, ElemType &e) 顺序表的取值
判断i值是否合理,若不合理,返回0;[i-1]单元存储第i个数据元素; 返回1

int LocateElem_Sq(SqList L, double e) 顺序表的查找
查找成功,返回序号i+1;查找失败,返回0

int ListInsert_Sq(SqList &L, int i, ElemType e)顺序表的插入
判断i值,不合法返回0;当前存储空间已满,返回0;插入位置及之后的元素后移;将新元素e放入第i个位置;表长增1;返回1

int ListDelete_Sq(SqList &L, int i)顺序表的删除
判断i值,不合法返回0;被删除元素之后的元素前移;表长减1;返回1

void ListInput(SqList &L)顺序表数据的输入
输入顺序表长度;依次输入数据;表长赋值

void ListOutput(SqList L)顺序表数据的输出
裁判测试程序样例:

include

include

include

include

using namespace std;

define OVERFLOW -2

typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型

define MAXSIZE 100 //顺序表可能达到的最大长度

typedef struct {
ElemType *elem; //存储空间的基地址
int length; //当前长度
} SqList;

void InitList_Sq(SqList &L) { //算法2.1 顺序表的初始化
//构造一个空的顺序表L
L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem)
exit(OVERFLOW); //存储分配失败退出
L.length = 0; //空表长度为0

}

/* 请在这里填写答案 */

int main() {
SqList L;
int i = 0, temp, a, c;
double price;
ElemType e;
//初始化线性表
InitList_Sq(L);
//输入线性表

        ListInput(L);
//输出线性表
        ListOutput(L);
//顺序表取值
        cin>>i;
        temp = GetElem(L, i, e);
        if (temp != 0) {
            cout <<"查找位置的数是"<<e<< endl;
        } else
            cout << "查找失败!位置超出范围\n";
//顺序表查找

        cin >> price;
        temp = LocateElem_Sq(L, price);
        if (temp != 0) {

            cout << "该数位置为" << temp << endl;
        } else
            cout << "查找失败!\n";

    //顺序表的插入

        cin >> a;
        cin >> e; //输入a和e,a代表插入的位置,e代表插入的数值
        if (ListInsert_Sq(L, a, e))
              ListOutput(L);
        else
            cout << "插入失败\n";
  
        //顺序表的删除

        cin >> c;
        if (ListDelete_Sq(L, c))
             ListOutput(L);
        else
            cout << "删除失败\n";


return 0;

}

输入样例:
在这里给出一组输入。例如:

5
5 4 3 2 1
2
2
2 6
6
输出样例:
在这里给出相应的输出。例如:

5 4 3 2 1
查找位置的数是4
该数位置为4
5 6 4 3 2 1
5 6 4 3 2 int GetElem(SqList L, int i, ElemType &e) {
if (i < 1 || i > L.length) return 0;
e = L.elem[i - 1];
return 1;
}

int LocateElem_Sq(SqList L, double e) {
for (int i = 0; i < L.length; ++i)
if (L.elem[i] == e) return i + 1;
return 0;
}

int ListInsert_Sq(SqList &L, int i, ElemType e) {
if (i < 1 || i > L.length + 1) return 0;
if (L.length == MAXSIZE) return 0;
for (int j = L.length; j >= i; --j)
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
++L.length;
return 1;
}

int ListDelete_Sq(SqList &L, int i) {
if (i < 1 || i > L.length) return 0;
for (int j = i; j < L.length; ++j)
L.elem[j - 1] = L.elem[j];
--L.length;
return 1;
}

void ListInput(SqList &L) {
cin >> L.length;
for (int i = 0; i < L.length; ++i)
cin >> L.elem[i];
}

void ListOutput(SqList L) {
for (int i = 0; i < L.length; ++i) {
cout << L.elem[i];
if (i != L.length - 1) cout << ' ';
}
cout << endl; // 增加换行符,与输出样例一致
}

posted @ 2025-10-29 22:53  muyuxiaxing  阅读(2)  评论(0)    收藏  举报