当前位置:网站首页>Basic operations of sequence table in C language
Basic operations of sequence table in C language
2022-07-30 09:53:00 【start little by little】
目录
6:顺序表的取值(根据位置iGets the content of the data element at the corresponding position)
顺序表的类型定义:
typedef struct{
ElemType data[];
//This way arrays are allocated statically;
//ElemType数据类型,可直接把ElemType 改为int、float......
也可以定义typedef int ElemType;
int length;
}SqList; //顺序表类型
//If the data type is composite,Not just a data type.Defining an abstract data type.
eg:
typedef struct{
float p;
int e;
}Polynomial;
typedef struct{
Polynomial *elem;
int length;
}SqList;
Dynamic allocation of complementary arrays
typedef struct{
ElemType *data;
int length;
}SqList;
//需要使用<stdlib.h>中的函数malloc与sizeof如下:
SqList L;
L.data = (ElemType *) malloc (sizeof(ElemType) * MaxSize); //Use this to dynamically allocate space.
顺序表的基本操作:
/*补充
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//Status 是函数的类型,其值是函数结果状态代码、
typedef int status;
typedef char ElemType;
1:顺序表L得初始化(参数引用)
Status IniList_Sq(SqList &L){
L.elem = new ElemType[MAXSIZE]; //new是C++函数与C的malloc函数作用一样:分配空间.
if(!L.elem) exit(OVERFLOW); //exit为函数,所在头文件为:stdlib.h 功能:关闭所有文件,终止正在执行的进程. exit(x)(x不为0)都表示异常退出.
L.length = 0;
return OK;
}
2:销毁顺序表L
void DestroyList(SqList &L){
if(L.elem)
delete L.elem; //释放存储空间
}
3:清空顺序表L
void ClearList(SqList &L){
L.length = 0; //将线性表的长度置为0;
}
4:求顺序表的长度
int getlength(SqList L){
return (L.length);
}
5:判断顺序表L是否为空
int IsEmpty(SqList L){
if(L.length == 0) return 1;
else return 0;
}
6:顺序表的取值(根据位置iGets the content of the data element at the corresponding position)
int GetElem(SqList L,int i,ElemType &e){
if(i<1 || i>length)
return ERROR; // 判断i值是否合理,若不合理,返回ERROR
e = L.elem[i-1]; //第i-1的单元存储着第i个数据
return OK;
}
7:在顺序表L中查找与指定值e相同的数据元素的位置
int LocateElem(SqList L,ELemType e){
for(i = 0; i<L.length; i++)
if(L.elem[i] == e) return i-1;
else return 0;
}
平均查找长度:ASL = (n+1)/2
8:在顺序表L中插入
status ListInsert_Sq(SqList &L, int i, ElemType e){
if(i<1 || i>L.length+1) return ERROR; //i不合法
if(L.length == MAXSIZE) return ERROR; //当前存储空间已满
for(j = L.length-1; j>=i-1; j++)
L.elem[j+1] = L.elem[j];
L.elem[i-1] = e;
L.length++;
return OK;
}
ASL = n/2 时间复杂度O(n)
9:顺序表中删除
status Listdelete_Sq(SqList &L,int i){
if(i<1) || i>L.length) return ERROR;
if(L.length == 0) return ERROR;
for(j =i; j<=length-1; j++)
L.elem[j-1] = L.elem[j];
L.length--;
return OK;
}
ASL = ( n-1)/2 时间复杂度O(n)
小结:
顺序表优缺点
优点:
1)存储密度大(结点本身所占存储量/结点结构所占存储量)
2)可以随机存取表中任一元素
缺点:
1)在插入、删除某一元素时,需要移动大量元素
2)浪费存储空间
3)属于静态存储形式,数据元素的个数不能自由扩充
边栏推荐
- Version management of public Jar packages
- leetcode 剑指 Offer 63. 股票的最大利润
- 编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
- Oracle 创建和操作表
- Circuit analysis: constant current source circuit composed of op amp and triode
- 聊聊 MySQL 事务二阶段提交
- Shell系统学习之数组
- 自动化测试selenium(一)
- Activating data potential Amazon cloud technology reshapes cloud storage "family bucket"
- leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
猜你喜欢
连接mysql报错WARN: Establishing SSL connection without server‘s identity verification is not recommended
【云原生】Kubernetes入门详细讲解
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
leetcode 剑指 Offer 46. 把数字翻译成字符串
Unified exception handling causes ResponseBodyAdvice to fail
conda 导出/导出配置好的虚拟环境
宝塔搭建DM企业建站系统源码实测
2022 Hangzhou Electric Multi-School 2nd Game
统一异常处理导致ResponseBodyAdvice失效
深入浅出零钱兑换问题——背包问题的套壳
随机推荐
经历了这样一个阶段的发展之后,数字零售才能有新的进化
echart图表清空上一次数据
pnpm简介
ESP32 入门篇(一)使用 VS Code 进行开发环境安装
详解JVM垃圾回收
聊聊 MySQL 事务二阶段提交
EViews 12.0软件安装包下载及安装教程
百度推广助手遇到重复关键字,验证错误,怎么一键删除多余的
Access to display the data
Unified exception handling causes ResponseBodyAdvice to fail
快解析结合友加畅捷通t1飞跃版
MySQL之COUNT性能到底如何?
柱状图 直方图 条形图 的区别
hcip06 ospf特殊区域综合实验
342 · Valley Sequence
The sword refers to offer 48: the longest non-repeating substring
C#中Config文件中,密码的 特殊符号的书写方法。
leetcode 剑指 Offer 25. 合并两个排序的链表
九九乘法表
Use the R language to read the csv file into a data frame, and then view the properties of each column.