当前位置:网站首页>c语言-线性顺序表
c语言-线性顺序表
2022-07-27 05:19:00 【an0420】
线性顺序表(1)
线性表
1.1什么是线性表以及顺序表?
线性表是包含若干元素的一个线性序列
例如:L=(a0、a1、…、ai-1、ai+1、…an)
L表名,ai(0<=i<=n)
n为表长,n>0时,线性表L为非空表,n=0时线性表为空表.
二元组形式为
L=(D,R)
大概来说线性表L包含数据元素集合D和关系R
D={ai| ai&datatype,i=0,1,2,…,n>=0}
R={<ai , ai+1>|ai , a+1&D , 0 <=i<=n-2}
<ai,ai+1>表示有序对表示相邻两个元素之间的一种先后次序,ai是ai+1的直接前驱,ai+1是ai的直接后继。
例如:有一个顺序表L={1,2,3,4,5,6};他们的关系为
1 - 2 - 3 - 4 - 5 -6
使用二元组表述L=(D,R)为
D={1,2,3,4,5,6}(n=6)
R={<12>、<23>、<34>、<45>、<56>}
可发现表表头1无前驱,表尾6无后继,其余每个元素仅有1个前驱和1个后继
1.2顺序存储结构表示
若将线性表L=(a0,a1,…,an-1)存储与计算机一片连续的存储空间内,假设Loc(ai)为ai的地址,Loc(a0)=b 则每个占d个单元的的元素表示为
Loc(ai)= b+i*d
1.3顺序存储的特征
优点
逻辑相邻的元素ai,ai+1存储位置也相邻
对于元素ai的存储为随机存储或按照地址存取(查找方便)
存储密度高,存储密度D=(数据结构中元素所占存储空间)/(整个数据空间)(使用率高)
缺点
对表的插入和删除等运算的时间复杂度较差
1.4顺序存储结构表示(简称顺序表)
在c语言中,可借助一维数组类型来描述线性表的顺序存储结构
例如
#define N 100
typedef int data_t;
typedef struct
{
data_t data[N];//表的存储空间
int last;
}sqlist,*sqlink;
顺序表的实现
线性表的基本运算
例:
设线性表L=(a0、a1、…、an-1)
(1)建立一个空表:list_create(L)
(2)置空表:list_clear(L)
(3)判断表是否为空:list_empty(L)。若表为空,返回值为1,否则返回值为0。
(4)求表长:length(L)
(5)却表中某个元素:GetList(L,i),即ai。0<=i<=length(L)-1
(6)定位运算:Locate(L,x)。确定元素在表中位置(序列号)
当 元素x=ai&L,且ai是第一个与x相等的返回i
当元素x不属于L时返回-1
(7)插入
lnsert(L,x,i),将x插入到表L中第i个元素ai之前,且表长+1。
编程格式
编程分层框架
常见的数据结构由3个文件构成
(1)sqlist.h
/* typedef int data_t; #define N 128 struct sqlist_t { data_t data[N]; int last; }; typedef struct sqlist_t sqlist;//sqlist L; struct sqlist_t L; typedef struct sqlist_t * sqlink;// struct sqlist_t * p; sqlink p; */
typedef int data_t;
#define N 128
typedef struct {
data_t data[N];
int last;
}sqlist, *sqlink;
sqlink list_create();
int list_clear(sqlink L);
int list_delete(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_locate(sqlink L, data_t value);
int list_insert(sqlink L, data_t value, int pos);
int list_show(sqlink L);
提供数据结构的定义,运算,接口
#include <stdio.h>
#include <string.h>
#include "sqlist.h"
sqlink list_create() {
sqlink L;
return L;
}
int list_clear(sqlink L) {
return 0;
}
int list_delete(sqlink L){
return 0;
}
int list_empty(sqlink L) {
return 0;
}
int list_length(sqlink L) {
return 0;
}
int list_locate(sqlink L, data_t value) {
return 0;
}
int list_insert(sqlink L, data_t value, int pos) {
return 0;
}
int list_show(sqlink L) {
return 0;
}
提供函数的实现
#include <stdio.h>
#include "sqlist.h"
int main(int argc, const char *argv[])
{
sqlink L;
L = list_create();
return 0;
}
函数的调用
优点:
结构清晰、软件复用性好(自身可直接调用、其他人员可用)、外包项目可用
初学者知识总结欢迎各位大佬补充纠正。
边栏推荐
- pytorch使用data_prefetcher提升数据读取速度
- 贪心高性能神经网络与AI芯片应用研修
- 15.GPU加速、minist测试实战和visdom可视化
- DSGAN退化网络
- 数字图像处理——第六章 彩色图像处理
- 【头歌】重生之我在py入门实训中(12):Matplotlib接口和常用图形
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- Day 17.The role of news sentiment in oil futures returns and volatility forecasting
- 李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22
- Uboot supports LCD and HDMI to display different logo images
猜你喜欢

15. GPU acceleration, Minist test practice and visdom visualization

16.过拟合欠拟合

关于pytorch反向传播的思考

Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease

方差与协方差
![[MySQL learning] 8](/img/25/84d5acbdd8aba3455ab8e3eb17dfa8.png)
[MySQL learning] 8

Pix2Pix原理解析

【Unity URP】代码获取当前URP配置UniversalRendererData,并动态添加RendererFeature
![[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog](/img/75/8f41db9f9c077b43751d63b7b5b57e.png)
[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog

13.逻辑回归
随机推荐
【头歌】重生之我在py入门实训中(12):Matplotlib接口和常用图形
dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
Day 8.Developing Simplified Chinese Psychological Linguistic Analysis Dictionary for Microblog
18. Convolutional neural network
【头歌】重生之我在py入门实训中(8): 模块
6. Dimension transformation and broadcasting
使用-Wall清除代码隐患
Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding
Day 11. Evidence for a mental health crisis in graduate education
西瓜书学习第五章---神经网络
【头歌】重生之我在py入门实训中(4):循环程序
Day 8.Developing Simplified Chinese Psychological Linguistic Analysis Dictionary for Microblog
新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic
【MVC架构】MVC模型
Unittest套件与运行器
Only one looper may be created per thread
Public opinion & spatio-temporal analysis of infectious diseases literature reading notes
个人开发者申请代码签名证书的签发流程
Gbase 8C - SQL reference 6 SQL syntax (10)
Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上