2024/9/27日工作日志

复习英语单词60个;
完成数据结构pta选择题,函数第一题;

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 GetElem(SqList L, int i, ElemType& e) {
if (i > L.length || i <= 0) {
return 0;
}
else
{
e = L.elem[i - 1];
return 1;
}
}

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

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

}

}

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

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

void ListOutput(SqList L) {
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << " ";
}
}

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;

}

posted @   张黎健  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示