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

C语言数据结构之顺序表

文章目录

  • 概念及结构
  • 接口实现

概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存
储。在数组上完成数据的增删查改

顺序表一般分为两种

静态:使用定长数组存储元素
动态:使用动态开辟的数组存储

在这里插入图片描述

#include <stdio.h>
#define N 10000
int main()
{typedef int SLdatetype;struct Seqlist {int a[N];int size;};return 0;
}

静态顺序表缺点:开多了用不了,开少了又不够

在这里插入图片描述

接口实现

头文件

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>typedef int SLdatetype;
#define int_capacity 4typedef	struct Seqlist{SLdatetype* array;size_t size;size_t capacity;}SL;void SLInit(SL* ps);//顺序表初始化
void checkcapacity(SL* ps);//检查空间大小,扩容void seqlistpushback(SL* ps,SLdatetype x);//尾插
void seqlistpopback(SL* ps);//尾割void seqlistpushfront(SL* ps,SLdatetype x);//头插
void seqlistpopfront(SL* ps);//头割SLdatetype seqlistfind(SL* ps, SLdatetype x);//查找xvoid seqlistinsert(SL* ps, SLdatetype pop,SLdatetype x);//在pop位置插入x
void seqlistrelease(SL* ps, SLdatetype pop);//在pop位置删除xvoid destory(SL* ps);//摧毁顺序表
void seqlistprint(SL* ps);//打印顺序表

函数实现

#include "seqlist.h"
void SLInit(SL* pch)
{pch->array = (SLdatetype*)malloc(sizeof(SLdatetype) * int_capacity);assert(pch);pch->size = 0;pch->capacity = int_capacity;
}
void destroy(SL* pch)
{free(pch->array);pch->array = NULL;pch->size = pch->capacity = 0;
}
void checkcapacity(SL* pch)
{if (pch->size == pch->capacity){SLdatetype* tmp = (SLdatetype*)malloc(sizeof(SLdatetype) * pch->capacity * 2);assert(tmp);pch->array = tmp;}pch->capacity *= 2;
}
void seqlistpushback(SL* pch, SLdatetype x){checkcapacity(pch);pch->array[pch->size++] = x;
}void seqlistpopback(SL* pch)
{assert(pch->size>0);pch->size--;
}
void seqlistprint(SL* pch)
{assert(pch);for (int i = 0;i < pch->size;i++){printf("%d ", pch->array[i]);}printf("\n");
}
void seqlistpushfront(SL* pch, SLdatetype x)
{assert(pch);checkcapacity(pch);int end = pch->size - 1;while (end>=0){pch->array[end+1] = pch->array[end];end--;}pch->array[0] = x;pch->size++;
}
void seqlistinsert(SL* pch, int pop, SLdatetype x)
{assert(pch);checkcapacity(pch);int end = pch->size - 1;while (end>=pop){pch->array[end + 1] = pch->array[end];end--;}pch->array[pop] = x;pch->size++;
}
void seqlistpopfront(SL* pch)
{assert(pch);int end = 0;while (end < pch->size-1){   pch->array[end] = pch->array[end+1];}pch->size--;
}
SLdatetype seqlistfind(SL* pch, SLdatetype x)
{assert(pch);for (int i = 0;i < pch->size;i++){if (pch->array[i] == x){return i;}}return -1;
}void seqlistrelease(SL* pch, SLdatetype pos)
{assert(pch);int end = pch->size - 1;int j = pos;while (j<end){pch->array[j] = pch->array[j + 1];j++;}pch->size--;
}

测试

#include "seqlist.h"
int main()
{SL s;SLInit(&s);seqlistpushback(&s, 1);seqlistpushback(&s, 2);seqlistpushback(&s, 3);seqlistpushback(&s, 4);seqlistpushback(&s, 5);seqlistpushback(&s, 6);seqlistpushback(&s, 7);seqlistprint(&s);seqlistpopback(&s);seqlistpopback(&s);seqlistpopback(&s);seqlistpopback(&s);seqlistprint(&s);seqlistpopback(&s);seqlistpushback(&s, 10);seqlistpushback(&s, 20);seqlistprint(&s);seqlistinsert(&s,2,9);seqlistprint(&s);seqlistpushfront(&s, 10);seqlistprint(&s);seqlistrelease(&s, 0);seqlistprint(&s);int c = seqlistfind(&s, 9);printf("%d", c);return 0;
}

在这里插入图片描述

生死有命富贵在天,能不能悟就看自己了

http://www.xdnf.cn/news/111943.html

相关文章:

  • 从代码学习深度学习 - 图像增广 PyTorch 版
  • 解决VSCode每次SSH连接服务器时,都需要下载vscode-server
  • Rust 2025:内存安全革命与异步编程新纪元
  • 大模型技术全景解析:从基础架构到Prompt工程
  • 无感字符编码原址转换术——系统内存(Mermaid文本图表版/DeepSeek)
  • 7.9 Python+Click实战:5步打造高效的GitHub监控CLI工具
  • #define STEUER_A_H {PWM_A_ON}
  • CSS3 基础(背景-文本效果)
  • 04-stm32的标准外设库
  • TI MSP430搭配 SD NAND(贴片式T卡):长续航心电监测的可靠保障
  • 关于按键映射软件的探索(其一)
  • STM32F407使用ESP8266实现阿里云OTA(下)
  • postgis:添加索引时提示“对访问方法 gist 数据类型 geometry 没有默认的操作符表“
  • 将视频生成视频二维码步骤
  • 深入浅出学会函数(下)
  • 【霍夫变换】图像处理(OpenCV)-part11
  • 【阿里云大模型高级工程师ACP习题集】2.4 自动化评测答疑机器人的表现(⭐️⭐️⭐️ 重点章节!!!)
  • 数据结构-图
  • HOW - Code Review 流程自动化
  • 学习threejs,使用EffectComposer后期处理组合器(采用RenderPass、ShaderPass渲染通道),案例一
  • 17.第二阶段x64游戏实战-人工遍历二叉树结构
  • 给git配置SSH(github,gitee)
  • 【前端】【业务场景】【面试】在前端项目中,当涉及大量数据渲染时,如何提高渲染性能并避免页面卡顿?
  • uniapp 安卓离线本地打包,Android Studio生成apk包
  • 27、Session有什么重⼤BUG?微软提出了什么⽅法加以解决?
  • Linux 命令行与 vi/vim 编辑器完全指南
  • continue插件实现IDEA接入本地离线部署的deepseek等大模型
  • 想要从视频中提取背景音乐怎么搞?其实视频提取音频非常简单
  • 金融系统上云之路:云原生后端架构在金融行业的演化与实践
  • 以太网的mac帧格式