当前位置: 首页 > news >正文

数据结构学习-第一天

初始化一个顺序表:

typedef struct{
	Elemtype date[MAXSIZE];
	int length;
}SeqList;
void InitList(SeqList *L){
	L->length=0;
}//初始化顺序表

输出:

int main(int argc,char const *argv[]){
	SeqList list;
	InitList(&list);
	return 0;
}

给顺序表添加数据:

void CreatElem(SeqList *L) {
    int n;
    cout << "请输入顺序表的长度: ";
    cin >> n;
    if (n > MAXSIZE) {
        cout << "超出最大容量!" << endl;
        return;
    }
    L->length = n;
    cout << "请输入" << n << "个元素: ";
    for (int i = 0; i < L->length; i++) {
        cin >> L->date[i];
    }
}

输出:

int main(int argc,char const *argv[]){
	SeqList list;
	InitList(&list);
	CreatElem(&list);
	return 0;
}

删除指定位置元素:

void DelElem(SeqList* L, int pos, Elemtype* e) {
    if (pos < 1 || pos > L->length) {
        cout << "删除位置错误" << endl;
        return;
    }
    *e = L->data[pos - 1];
    if (pos < L->length) {
        for (int i = pos; i < L->length; i++) {
            L->data[i - 1] = L->data[i];
        }
        L->length--;
    }
}

输出:

int main(int argc,char const *argv[]){
	SeqList list;
	InitList(&list);
	CreatElem(&list);
	int n;
	cout <<"你想删除哪个位置的元素: ";
	cin >> n;
	Elemtype delDate;
	deleteElem(&list,n,&delDate);
	printf("被删除的数据为:%d\n",delDate);
	
	return 0;
}

顺序表整个的输出函数:

void listElem(SeqList* L) {
	for (int i = 0; i < L->length; i++) {
		cout << L->data[i]<<' ';
	}
	cout << endl;
}

全部输出:

int main(int argc, char const* argv[]) {
	SeqList list;
	IniList(&list);
	CreateElem(&list);
	int count;
	cout << "你想执行什么操作" << endl;
	cout << "删除指定位置元素:1" << endl;

	cin >> count;
	if (count == 1) {
		int pos;
		Elemtype DelData;
		cout << "你想删除什么位置的元素:";
		cin >> pos;
		DelElem(&list, pos, &DelData);
		cout << "删除的元素是:" << DelData << endl;
		cout << "删除指定元素后的顺序表:" << endl;
		listElem(&list);
	}
	return 0;
}

插入指定元素:

void InsertElem(SeqList* L, int pos, Elemtype e) {
    if (L->length >= MAXSIZE) {
        cout << "顺序表已经满了" << endl;
        return;
    }
    if (pos < 1 || pos > L->length + 1) {
        cout << "插入位置错误" << endl;
        return;
    }
    if (pos <= L->length) {
        for (int i = L->length - 1; i >= pos - 1; i--) {
            L->data[i + 1] = L->data[i];
        }
        L->data[pos - 1] = e;
        L->length++;
    }
}

全部输出:

int main(int argc, char const* argv[]) {
    SeqList list;
    IniList(&list);
    CreateElem(&list);
    int count;
    cout << "你想执行什么操作" << endl;
    cout << "删除指定位置元素:1" << endl;
    cout << "插入指定位置元素:2" << endl;

    cin >> count;
    if (count == 1) {
        int pos;
        Elemtype DelData;
        cout << "你想删除什么位置的元素:";
        cin >> pos;
        DelElem(&list, pos, &DelData);
        cout << "删除的元素是:" << DelData << endl;
        cout << "删除指定元素后的顺序表:" << endl;
        listElem(&list);
    }
    if (count == 2) {
        int pos;
        Elemtype InsertData;
        cout << "你想插入什么位置:";
        cin >> pos;
        cout << "该位置插入什么元素:";
        cin >> InsertData;
        InsertElem(&list, pos, InsertData);

        cout << "插入指定元素后的顺序表:" << endl;
        listElem(&list);
    }

    return 0;
}

相关文章:

  • 【注解简化配置的原理是什么】
  • springboot-ai接入DeepSeek
  • NO.71十六届蓝桥杯备战|搜索算法-递归型枚举与回溯剪枝|枚举子集|组合型枚举|枚举排列|全排列问题(C++)
  • SpringBoot自动装配原理---Spring
  • import cv2 安装失败
  • 语法: value=label_address( label);
  • PyTorch池化层详解:原理、实现与示例
  • ctf-show-micsx
  • 【Kubernetes】StorageClass 的作用是什么?如何实现动态存储供应?
  • TLS 1.2 握手过程,每个阶段如何保证通信安全?​​
  • 古诗词数据集(74602条简体版、繁体版) | 智能体知识库 | AI大模型训练
  • iOS APP集成Python解释器
  • OpenCV 在树莓派上进行实时人脸检测
  • C++ 内存访问模式优化:从架构到实践
  • Redis之布隆过滤器
  • Unity3D仿星露谷物语开发34之单击Drop项目
  • 算法思想之滑动窗口(一)
  • 人脸专注度检测系统(课堂专注度检测、人脸检测、注意力检测系统)
  • 【C++】第九节—string类(中)——详解+代码示例
  • JVM深入原理(六)(一):JVM类加载器
  • 试描述一下网站建设的基本流程图/seo是什么的简称
  • 简约大气网站/成都关键词优化平台
  • yollow网站推广/谷歌chrome浏览器下载
  • 网站被百度k掉怎么办/中国新闻今日头条
  • 做网站视频用哪个视频编辑软件/热狗网站关键词优化
  • 做小说网站做国外域名还是国内的好/seo 公司