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

01_线性表

一、线性表的顺序存储

逻辑上相邻的数据元素,物理次序也相邻。占用连续存储空间,用“数组”实现,知道初始位置就可推出其他位置。

00_宏定义

// 函数结果状态代码
#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;

01_构建存储结构

// 表的名称和长度
#define SQLMAXSIZE 100;
typedef int SqlElemType;
/*
typedef struct {float p;int e;
}SqlElemType;
*/
typedef struct __Sqlist{SqlElemType *base;   // 存储空间的基地址,后面用malloc动态分配内存int length;
}Sqlist;

备注:int max(int a[])和int max(int a[100])和int max(int a[0])三者等效。

02_初始化线性表

Status InitSL(Sqlist *L, int length){L->base = (SqlElemType *)malloc(sizeof(SqlElemType) * SQLMAXSIZE);if (!L->base) return OVERFLOW;L->length = 0;    for (int i = 0; i < length + 1; i++){SqlElemType e;scanf(" %d", &e);SqlInsert(L, i, e);}return OK;
}

03_获取第position个元素

Status GetElem(Sqlist *L, int position, SqlElemType *e){if (position < 1 || position > L->length)return ERROR;*e = L->base[position -1];return OK;
}

04_查找与给定元素e相同的元素

Status LocatElem(Sqlist *L ,SqlElemType e){for (int i = 0; i < L->length, i++){if (e == L->base[i])return i + 1;}return 0; //如果元素不在循环里
}

05_在某个位置插入元素

Status SqlInsert(Sqlist *L, int position, SqlElemType *e){if (position < 1 || position > L->length + 1) return ERROR;if (L->length == SQLMAXSIZE)return OVERFLOW;for (int i = L->length - 1; i >= position - 1; i--){L->base[i+1] = L->base[i];}L->base[position - 1] = e;L->length ++;return OK;
}

06_删除某个位置上的元素

Status SqDelete(Sqlist *L, int position, SqlElemType *e){if (posiiton < 1 || position > L->length)return ERROR;for (int i = position ; i <= L->length - 1; i ++ ){L->base[i - 1] =  L->base[i];}*e = L->base[position - 1];L->length --;return OK;
}

07_销毁线性表

Status SqDestory(Sqlist *L){if (!L->base) return ERROR;else{free(L->base);  // free的输入参数是指针return OK;}
}

08_清空线性表

void SqClear(Sqlist *L){L->base = 0;
}

09_检查线性表是否为空

Status SqEmpty(Sqlist *L){if (0 == L->length) return TRUE;else return FALSE;
}

相关文章:

  • Java中的Classpath 包含哪些目录?
  • linux -shell原理与运用
  • openwrt 使用quilt 打补丁(patch)
  • 【Harbor v2.13.0 详细安装步骤 安装证书启用 HTTPS】
  • WebRTC并非万能:RTMP与RTSP的工程级价值再认识
  • Flutter开发IOS蓝牙APP的大坑
  • 【2025】Visio 2024安装教程保姆级一键安装教程(附安装包)
  • 【奔跑吧!Linux 内核(第二版)】第1章:Linux 系统基础知识
  • Ros工作空间
  • IDEA 占用C盘太大清理
  • LangChain:大语言模型应用的“瑞士军刀”入门指南
  • 上市公司-企业上下游供应链数据(2003-2023年)-社科数据
  • 推导部分和-图论+dfs+连通块
  • 【数据挖掘】Apriori算法
  • 主数据 × 知识图谱:打造企业认知智能的核心基础设施
  • 全国青少年信息素养大赛 Python编程挑战赛初赛 内部集训模拟试卷一及详细答案解析
  • 孤岛铜怎么解决
  • 极狐GitLab 如何将项目共享给群组?
  • 极狐Gitlab 里程碑功能介绍
  • 手写 Vue 源码 === Effect 机制解析
  • 王耀庆化身“罗朱”说书人,一人挑战15个角色
  • 长三角多地重启游轮跨市游,“恢复苏杭夜航船”呼声又起
  • 巴基斯坦军方:印度向巴本土及巴控克什米尔发射导弹
  • 商务部:自5月7日起对原产于印度的进口氯氰菊酯征收反倾销税
  • 工人日报关注跟着演出去旅游:票根经济新模式兴起,让过路客变过夜客
  • 山大齐鲁医院护士论文现“男性确诊子宫肌瘤”,院方称将核实