当前位置:网站首页>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;
}
函数的调用
优点:
结构清晰、软件复用性好(自身可直接调用、其他人员可用)、外包项目可用
初学者知识总结欢迎各位大佬补充纠正。
边栏推荐
- Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
- pytorch使用data_prefetcher提升数据读取速度
- 17. Attenuation of momentum and learning rate
- Gbase 8C - SQL reference 4 character set support
- 13.逻辑回归
- 为什么交叉熵损失可以用于刻画损失
- Gbase 8C - SQL reference 6 SQL syntax (2)
- 【头歌】重生之深度学习篇-Keras(初级)
- [Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog
- 16.过拟合欠拟合
猜你喜欢

Greedy high performance neural network and AI chip application research and training

19.上下采样与BatchNorm

Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

3. Classification problems - initial experience of handwritten digit recognition

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

Digital image processing Chapter 5 - image restoration and reconstruction

Digital image processing Chapter 8 - image compression

西瓜书第三章---线性模型学习笔记

【好文种草】根域名的知识 - 阮一峰的网络日志
随机推荐
Gbase 8C - SQL reference 5 full text search
【头歌】重生之我在py入门实训中(5):列表
Public opinion & spatio-temporal analysis of infectious diseases literature reading notes
6.维度变换和Broadcasting
Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
Activity之应用进程创建流程简析
15. GPU acceleration, Minist test practice and visdom visualization
Gbase 8C - SQL reference 6 SQL syntax (4)
Global evidence of expressed sentimental alterations during the covid-19 pandemics
cycleGAN解析
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
8.数学运算与属性统计
9.高阶操作
数字图像处理——第九章 形态学图像处理
Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease
Andorid detects GPU rendering speed and over rendering
5. Indexing and slicing
Digital image processing Chapter 5 - image restoration and reconstruction
服务器相关的指标解释
7.合并与分割