今天给大家分享我学习数据结构的学习成果:
用C++实现线性表的顺序存储操作:
- 首先定义一个结构体类型的变量(不要忘记头文件!!!)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> //定义一个常量 #define MaxSize 15 //顺序表中的元素类型 typedef int ElemType; //定义一个线性表 typedef struct { ElemType data[MaxSize]; int len; }SqList;用typedef给类型起别名,方便以后在顺序表放入不同的类型数据
-
然后就是定义函数的步骤了
/定义一个插入函数 //返回布尔值 bool ListInsert(SqList& L, int i, ElemType e) { if (i<1 || i>L.len + 1) { return false; } if (L.len >= MaxSize) { return false; } for (int j = L.len; j >= i; j--) { L.data[j] = L.data[j - 1]; } L.data[i - 1] = e; L.len++; return true; }当然,此代码的原理想必各位大佬都比我熟悉,不过多赘述了。删除操作、查找操作代码如下:
//定义一个删除函数 //返回布尔值 bool ListDelete(SqList& L, int i) { //ElemType e = 0; if (i<1 || i>L.len) { return false; } if (0 == L.len) { retur

本文介绍了使用C++编程实现线性表的顺序存储结构,包括增删查等基本操作。通过定义结构体和typedef简化类型,详细展示了相关函数的实现。
最低0.47元/天 解锁文章
157

被折叠的 条评论
为什么被折叠?



