数据结构-Data Structure (顺序表专题)
标题数据结构概念数据结构是计算机存储、组织数据的方式。它是指相互之间存在⼀种或多种特定关系的数据元素的集合。分为线性结构和非线性结构。程序中如果不对数据进⾏管理可能会导致数据丢失、操作数据困难、野指针等情况。通过数据结构能够有效将数据组织和管理在⼀起。按照自己的方式可以任意对数据进行增删改查等操作。线性结构概念线性结构是数据元素之间存在一对一线性关系的数据结构所有元素按前后次序排成一条连续的 “线性序列”。线性结构在物理结构上不一定是连续的但在逻辑结构上一定是连续的。常见类型顺序表列表栈队列字符串,,,顺序表概念顺序表是采用一段连续的内存空间依次存储数据元素的线性结构基于数组实现元素的逻辑顺序与物理存储顺序完全一致。分类静态顺序表概念使用定长数组存储元素。#define N 100 typedef struct Seqlist { int arr[N]; //定长数组 int size; //定义有效数据的个数 }SL;动态顺序表typedef struct Seqlist { int* arr; //定长数组 int size; //定义有效数据的个数 int capacity; //定义可变空间大小 }SL;动态顺序表的实现创建//定义顺序表的结构 typedef int SLtype; //定义宏 顺序表类型 //定义动态顺序表 typedef struct Seqlist { SLtype* arr; SLtype size; SLtype capacity; }SL;初始化//初始化 void SLinit(SL* ps); //定义指针变量来接受地址传参进行初始化 void SLinit(SL* ps) { ps-arr NULL; ps-size ps-capacity 0; }打印//打印 void SLprint(SL s); void SLprint(SL s) { for ( int i 0; i s.size; i) { printf(%d , s.arr[i]); } printf(\n); }插入//插入 void SLpushback(SL* ps, SLtype x); //尾部插入 void SLpushfront(SL* ps, SLtype x); //头部插入 void checkcapacity(SL* ps) { if (ps-capacity ps-size) //如果相等需要申请空间 { //malloc calloc relloc- 涉及增容 //三目表达式 //将初始化的capacity空间大小设值,若为0初始化为4若不为0设值为两倍增容 int newcapacity ps-capacity 0 ? 4 : 2 * ps-capacity; //增容一般使用倍数增容方式这里使用最常见的两倍增容。 SLtype* tmp (SLtype*)realloc(ps-arr, newcapacity * 2 * sizeof(SLtype)); if (tmp NULL) { perror(realloc fail!); exit(1); } ps-arr tmp; ps-capacity newcapacity; } } void SLpushback(SL* ps, SLtype x) { /*ps-arr[ps-size] x; ps-size;*/ /*if (ps NULL) { return; }*/ assert(ps); //判断空间大小是否足够 checkcapacity(ps); ps-arr[ps-size] x; } void SLpushfront(SL* ps, SLtype x) { assert(ps); checkcapacity(ps); //将顺序表数据整体向后挪动 for ( int i ps-size ;i0;i--) { ps-arr[i] ps-arr[i - 1]; } ps-arr[0] x; ps-size; }删除//删除 void SLpopback(SL* ps); //尾部删除 void SLpopfront(SL* ps); //头部删除 void SLpopback(SL* ps) { assert(ps); //判断顺序表是否为空 assert(ps-size); //ps-arr[ps-size - 1] -1; --ps-size; } void SLpopfront(SL* ps) { assert(ps); assert(ps-size); for ( int i 0; i ps-size-1; i) { ps-arr[i] ps-arr[i 1]; } ps-size--; }销毁//销毁 void SLdestroy(SL* ps); void SLdestroy(SL* ps) { if (ps-arr) { free(ps-arr); } ps-arr NULL; ps-size ps-capacity 0; }指定位置插入//指定位置插入 void SLInsert(SL* ps, int pos, SLType x); void SLInsert(SL* ps, int pos, SLType x) { assert(ps); assert(pos 0 pos ps-size); SLCheckCapacity(ps); for (int i ps-size; i pos; i--) { ps-arr[i] ps-arr[i - 1]; } ps-arr[pos] x; ps-size; }指定位置删除//指定位置删除 void SLErase(SL* ps, int pos); void SLErase(SL* ps, int pos) { assert(ps); assert(pos 0 pos ps-size); for (int i pos; i ps-size-1; i) { ps-arr[i] ps-arr[i 1]; } ps-size--; }查找//查找 int SLFind(SL* ps, SLType x); int SLFind(SL* ps, SLType x) { assert(ps); for ( int i 0; i ps-size; i) { if (ps-arr[i] x) { return i; } } return -1; } //测试 int find SLFind(s1,3); if (find 0) { printf(no found!); } else { printf(find it! The subscript is %d, find); }完整代码如下1. 顺序表头文件 seqlist.h 定义#pragma once #include stdio.h #include stdlib.h #include assert.h //顺序表 //创建 typedef int SLType; //指定类型 typedef struct seqlist { SLType* arr; int size; int capacity; }SL; //指定顺序表名称为SL //初始化 void SLInit(SL* ps); //打印 void SLPrint(SL s); //销毁 void SLDestroy(SL* ps); //头插 void SLPushFront(SL* ps,SLType x); //尾插 void SLPushBack(SL* ps, SLType x); //头删 void SLPopFront(SL* ps); //尾删 void SLPopBack(SL* ps); //指定位置插入 void SLInsert(SL* ps, int pos, SLType x); //指定位置删除 void SLErase(SL* ps, int pos); //查找 int SLFind(SL* ps, SLType x);2.执行源文件seqlist.c#define _CRT_SECURE_NO_WARNINGS 1 #include seqlist.h //初始化 void SLInit(SL* ps) { ps-arr NULL; ps-capacity ps-size 0; } //打印 void SLPrint(SL s) { for (int i 0; i s.size; i) { printf(%d , s.arr[i]); } printf(\n); } //销毁 void SLDestroy(SL* ps) { if (ps-arr) { free(ps-arr); } ps-size ps-capacity 0; } //申请空间 void SLCheckCapacity(SL* ps) { if (ps-sizeps-capacity) { int newcapacity ps-capacity 0 ? 4 : 2 * ps-capacity; SLType* tmp (SLType*)realloc(ps-arr, newcapacity*sizeof(SLType)); if (tmp NULL) { perror(realloc fail!); } ps-arr tmp; ps-capacity newcapacity; } } //头插 void SLPushFront(SL* ps, SLType x) { //申请空间 assert(ps); SLCheckCapacity(ps); for (int ips-size;i0;i--) { ps-arr[i] ps-arr[i - 1]; } ps-arr[0] x; ps-size; } //尾插 void SLPushBack(SL* ps, SLType x) { assert(ps); SLCheckCapacity(ps); ps-arr[ps-size] x; } //头删 void SLPopFront(SL* ps) { assert(ps); assert(ps-size); for (int i 0; i ps-size-1; i) { ps-arr[i] ps-arr[i 1]; } ps-size--; } //尾删 void SLPopBack(SL* ps) { assert(ps); assert(ps-size); --ps-size; } //指定位置插入 void SLInsert(SL* ps, int pos, SLType x) { assert(ps); assert(pos 0 pos ps-size); SLCheckCapacity(ps); for (int i ps-size; i pos; i--) { ps-arr[i] ps-arr[i - 1]; } ps-arr[pos] x; ps-size; } //指定位置删除 void SLErase(SL* ps, int pos) { assert(ps); assert(pos 0 pos ps-size); for (int i pos; i ps-size-1; i) { ps-arr[i] ps-arr[i 1]; } ps-size--; } //查找 int SLFind(SL* ps, SLType x) { assert(ps); for ( int i 0; i ps-size; i) { if (ps-arr[i] x) { return i; } } return -1; }3.测试文件test.c#define _CRT_SECURE_NO_WARNINGS 1 #include seqlist.h void test01() { SL s1; SLInit(s1); SLPushBack(s1, 1); SLPushBack(s1, 2); SLPrint(s1); SLPushFront(s1, 3); SLPushFront(s1, 4); SLPrint(s1); SLPopBack(s1); SLPrint(s1); SLPopFront(s1); SLPrint(s1); SLInsert(s1, 0, 6); SLPrint(s1); SLInsert(s1,s1.size, 9); SLPrint(s1); SLInsert(s1, 1, 7); SLPrint(s1); SLErase(s1, 3); SLPrint(s1); SLErase(s1, s1.size); SLPrint(s1); SLErase(s1, 0); SLPrint(s1); int find SLFind(s1,3); if (find 0) { printf(no found!); } else { printf(find it! The subscript is %d, find); } } int main() { test01(); return 0; }代码运行结果如下图所示通讯录项目数据结构设计定义通讯录中联系人的结构体如姓名、电话、地址等字段顺序表的存储结构及容量管理策略静态数组或动态扩容核心功能实现初始化通讯录分配内存或设置初始容量添加联系人检查容量并插入数据删除联系人查找并移除数据处理后续元素移位查找联系人按姓名或关键字遍历搜索修改联系人信息定位后更新字段显示所有联系人遍历输出代码实现定义通讯录结构体 c.h#pragma once //定义通讯录联系人结构体 //姓名 性别 年龄 电话 地址 #define NM 20 #define GM 10 #define AM #define TM 20 #define ADM 100 typedef struct PersonInfo { char name[NM]; char gender[GM]; int age; char tel[TM]; char addr[ADM]; }Peo;在顺序表头文件当中指定类型 s.htypedef Peo SLType; //指定类型对通讯录进行操作 c.h//对通讯录进行操作 // //前置声明 typedef struct seqlist contact; //初始化 void ContactInit(contact* con); //销毁 void ContactDestroy(contact* con); //插入数据 void ContactInsert(contact* con); //删除数据 void ContactErase(contact* con); //修改数据 void ContactModify(contact* con); //查找数据 void ContactFind(contact* con); //打印显示 void ContactShow(contact* con);通讯录初始化 .cvoid ContactInit(contact* con) { SLInit(con); //直接调用即可 }添加数据void ContactAdd(contact* con) { //获取用户输入信息 姓名 性别 年龄 电话 住址 Peo info; printf(please input name:\n); scanf(%s, info.name); printf(please input gender:\n); scanf(%s, info.gender); printf(please input age:\n); scanf(%d, info.age); printf(please input telephone:\n); scanf(%s, info.tel); printf(please input address:\n); scanf(%s, info.addr); //添加数据 SLPushBack(con, info); }删除数据int FindName(contact* con,char name[]) { for (int i 0; i con-size; i) { if (0 strcmp(con-arr[i].name,name)) //找到 { return i; } } return -1; //没有找到 } void ContactErase(contact* con) { char name[NM]; printf(please input name that need delete:\n); scanf(%s, name); //利用查找方式判断数据是否存在 int find FindName(con, name); if (find 0) { printf(not found!\n); return; } SLErase(con, find); //根据返回下标执行删除 printf(delete done!\n); }修改数据void ContactModify(contact* con) { char name[NM]; printf(please input name that need modify:\n); scanf(%s, name); int find FindName(con, name); if (find 0) { printf(not found!\n); return; } printf(please input new name:\n); scanf(%s, con-arr[find].name); printf(please input new gender:\n); scanf(%s, con-arr[find].gender); printf(please input new age:\n); scanf(%d, con-arr[find].age); printf(please input new telephone:\n); scanf(%s, con-arr[find].tel); printf(please input new address:\n); scanf(%s, con-arr[find].addr); }查找数据void ContactFind(contact* con) { char name[NM]; printf(please input name that need find\n); scanf(%s, name); int find FindName(con, name); if (find 0) { printf(not found!\n); return; } //表头打印 printf(%s %s %s %s %s\n, 姓名, 性别, 年龄, 电话, 住址); printf(%s %s %d %s %s\n, con-arr[find].name, con-arr[find].gender, con-arr[find].age, con-arr[find].tel, con-arr[find].addr ); }展示数据void ContactShow(contact* con) { printf(%s %s %s %s %s\n, 姓名, 性别, 年龄, 电话, 住址); for (int i 0; i con-size; i) { printf(%s %s %d %s %s\n, con-arr[i].name, con-arr[i].gender, con-arr[i].age, con-arr[i].tel, con-arr[i].addr ); } }通讯录销毁void ContactDestroy(contact* con) { SLDestroy(con); }建立通讯录菜单void menu() { printf(*******************通讯录******************\n); printf(********1.添加联系人 2.删除联系人*********\n); printf(********3.修改联系人 4.查找联系人*********\n); printf(********5.展示联系人 0.退出系统 *********\n); printf(*******************************************\n); }部分效果展示主函数执行代码如下int main() { int op -1; contact con; ContactInit(con); do{ menu(); printf(please choose a option!\n); scanf(%d, op); switch (op) { case 1: ContactAdd(con); break; case 2: ContactErase(con); break; case 3: ContactModify(con); break; case 4: ContactFind(con); break; case 5: ContactShow(con); break; case 0: printf(exit!\n); break; default: break; } } while (op !0 ); ContactDestroy(con); return 0; }

相关新闻

免费PDF工具实测:PDF补丁丁三步上手,书签合并提取一次搞定

免费PDF工具实测:PDF补丁丁三步上手,书签合并提取一次搞定

免费PDF工具实测:PDF补丁丁三步上手,书签合并提取一次搞定 【免费下载链接】PDFPatcher PDF补丁丁——PDF工具箱,可以编辑书签、剪裁旋转页面、解除限制、提取或合并文档,探查文档结构,提取图片、转成图片等等 项目地…

2026/8/15 15:39:36 阅读更多 →
mini小巧-WIFI-温湿度检测

mini小巧-WIFI-温湿度检测

项目简介mini小巧-WIFI-温湿度检测使用STC8G1K08A 大夏龙雀WIFI模块EPS8266蓝牙,搭配AHT30 温湿度模块,通过使用MQTT协议上传数据。本设计简单易上手归于大夏龙雀wifi/蓝牙二合一模块集成TCP/UDP/MQTT 协议通信省去繁杂程序开发与测试。项目功能本设计是…

2026/8/15 15:39:36 阅读更多 →
每天更新114个:这份公共Tracker服务器列表,是怎么把BT下载从“0KB/s“救回来的

每天更新114个:这份公共Tracker服务器列表,是怎么把BT下载从“0KB/s“救回来的

每天更新114个:这份公共Tracker服务器列表,是怎么把BT下载从"0KB/s"救回来的 【免费下载链接】trackerslist Updated list of public BitTorrent trackers 项目地址: https://gitcode.com/GitHub_Trending/tr/trackerslist 那天晚上十一…

2026/8/15 15:39:36 阅读更多 →

最新新闻

终极解析Make-An-Audio配置文件:txt2audio_args.yaml参数调优全攻略

终极解析Make-An-Audio配置文件:txt2audio_args.yaml参数调优全攻略

终极解析Make-An-Audio配置文件:txt2audio_args.yaml参数调优全攻略 【免费下载链接】Make-An-Audio PyTorch Implementation of Make-An-Audio (ICML23) with a Text-to-Audio Generative Model 项目地址: https://gitcode.com/gh_mirrors/ma/Make-An-Audio …

2026/8/15 16:22:55 阅读更多 →
Horos开源医学影像查看器深度解析:从DICOM数据模型到三维重建的完整实战

Horos开源医学影像查看器深度解析:从DICOM数据模型到三维重建的完整实战

Horos开源医学影像查看器深度解析:从DICOM数据模型到三维重建的完整实战 【免费下载链接】horos Horos™ is a free, open source medical image viewer. The goal of the Horos Project is to develop a fully functional, 64-bit medical image viewer for OS X. …

2026/8/15 16:22:55 阅读更多 →
boolinq常见问题解答:新手必知的10个坑

boolinq常见问题解答:新手必知的10个坑

boolinq常见问题解答:新手必知的10个坑 【免费下载链接】boolinq Simplest C header-only LINQ template library 项目地址: https://gitcode.com/gh_mirrors/bo/boolinq boolinq是一款轻量级C头文件库,它提供了类似LINQ的便捷操作,让…

2026/8/15 16:22:55 阅读更多 →
3步免装iTunes搞定苹果USB驱动:Windows下iPhone网络共享不再掉链子

3步免装iTunes搞定苹果USB驱动:Windows下iPhone网络共享不再掉链子

3步免装iTunes搞定苹果USB驱动:Windows下iPhone网络共享不再掉链子 【免费下载链接】Apple-Mobile-Drivers-Installer Powershell script to easily install Apple USB and Mobile Device Ethernet (USB Tethering) drivers on Windows! 项目地址: https://gitcod…

2026/8/15 16:22:55 阅读更多 →
把浏览器“编译”进CPU:Thorium(钍浏览器)从安装到调优的完整体验记录

把浏览器“编译”进CPU:Thorium(钍浏览器)从安装到调优的完整体验记录

把浏览器“编译”进CPU:Thorium(钍浏览器)从安装到调优的完整体验记录 【免费下载链接】thorium Chromium fork named after radioactive element No. 90. Source code and Linux releases. Windows/MacOS/ARM builds served in different re…

2026/8/15 16:22:55 阅读更多 →
30 天迁移日志:用 PhotoGIMP 把 Photoshop 换成免费图像编辑

30 天迁移日志:用 PhotoGIMP 把 Photoshop 换成免费图像编辑

30 天迁移日志:用 PhotoGIMP 把 Photoshop 换成免费图像编辑 【免费下载链接】PhotoGIMP A Patch for GIMP 3 for Photoshop Users 项目地址: https://gitcode.com/GitHub_Trending/ph/PhotoGIMP 这是我的亲历日志。第 0 天,我的 Photoshop 订阅即…

2026/8/15 16:21:55 阅读更多 →

日新闻

内景 空间站内部 中国空间站 太空 内仓

内景 空间站内部 中国空间站 太空 内仓

本项目为前几天收费帮学妹做的一个项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 空间站内部 中国空间站 太空 内仓 地址:本地PC端运行(或Web…

2026/8/15 0:00:30 阅读更多 →
重新定义数据接口:3个突破性场景让通达信数据读取更智能

重新定义数据接口:3个突破性场景让通达信数据读取更智能

重新定义数据接口:3个突破性场景让通达信数据读取更智能 【免费下载链接】mootdx 通达信数据读取的一个简便使用封装 项目地址: https://gitcode.com/GitHub_Trending/mo/mootdx 当我们面对海量金融数据时,传统的数据获取方式往往让我们陷入困境—…

2026/8/15 0:00:30 阅读更多 →
一文读懂快消WMS怎么选?2026年国内外10大主流WMS品牌盘点

一文读懂快消WMS怎么选?2026年国内外10大主流WMS品牌盘点

快消品(FMCG)是流通速度较快、竞争较为激烈的行业之一。一瓶饮料从出厂到消费者手中,往往只有几十天甚至几天的周转窗口。这决定了快消行业的仓储管理系统(WMS)与制造业、电商行业存在明显区别:它不仅需要管…

2026/8/15 0:02:30 阅读更多 →

周新闻

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁 【免费下载链接】baidupankey 在线查询网盘提取码(维护中 rm repo) 项目地址: https://gitcode.com/gh_mirrors/ba/baidupankey 你是否曾经在深夜寻找一份重要资料&#x…

2026/8/13 2:38:34 阅读更多 →
如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南 【免费下载链接】chinese_license_plate_generator 中国车牌生成器 项目地址: https://gitcode.com/gh_mirrors/ch/chinese_license_plate_generator 中国车牌生成器是一个基于Python的开源项目&#xff0c…

2026/8/15 12:59:14 阅读更多 →
收藏!小白程序员轻松入门大模型,从Harness工程开始实践

收藏!小白程序员轻松入门大模型,从Harness工程开始实践

文章强调学习大模型不应只关注模型本身,而应重视模型外的系统搭建,即Harness。提出AgentModelHarness的实用公式,详细介绍Harness的四个层次:持久化层、执行层、控制层和观察与验证层。文章还探讨了上下文工程、工具设计、AGENTS.…

2026/8/13 10:41:51 阅读更多 →

月新闻

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 还在为百度网盘macOS版的龟速下…

2026/8/14 13:40:53 阅读更多 →
终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐下载的NCM格式文件无法在其他播放器播放而烦恼吗?ncmdump解密工具帮你轻松解决这个困…

2026/8/14 14:06:45 阅读更多 →
HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

AgentCard 智能体卡片:为英语学习 App 打造桌面级学习助手适用平台:HarmonyOS 7.0 (API 26 Beta)一、引言 HarmonyOS 7.0(API 26 Beta)新增了 AgentCard 智能体卡片能力,这是继 HMAF(鸿蒙智能体框架&#x…

2026/8/15 2:35:29 阅读更多 →