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

数据结构:顺序表

目录

结构体

顺序表存储数据类型结构体

 顺序表结构体

函数 

初始化顺序表

判断顺序表是否存满

判断顺序表是否为空

顺序表插入新元素

在指定位置插入新元素

遍历顺序表所有元素,对每个元素完成指定操作

      (指定操作1)  打印所有元素

        (指定操作2)更新指定元素

        (指定操作3)找到指定元素的位置   

删除指定位置的元素

清零顺序表

销毁顺序表


 

结构体

顺序表存储数据类型结构体

typedef int DataType;

 顺序表结构体

typedef struct seqlist
{DataType *data;  //存储数据int clen;        //顺序表存储的的数据个数int tlen;        //顺序表的大小
}Seqlist;

函数 

初始化顺序表

(返回结构体指针)

Seqlist *InitSeqlist(int maxlen)
{Seqlist *tmpseqlist=NULL;
//申请顺序表结构体指针tmpseqlist=malloc(sizeof(*tmpseqlist));if(tmpseqlist==NULL){return NULL;}tmpseqlist->clen=0;tmpseqlist->tlen=maxlen;
//申请存储数据的空间tmpseqlist->data=malloc(maxlen * sizeof(DataType));if(tmpseqlist->data==NULL){return NULL;}return tmpseqlist;
}

判断顺序表是否存满

(存满返回1,没满返回0)

int IsFullSeqlist(Seqlist *pseqlist)
{return pseqlist->clen == pseqlist->tlen?1:0;
}

判断顺序表是否为空

(为空返回1,否则0)

int EmptySeqlist(Seqlist *pseqlist)
{return pseqlist->clen==0?1:0;
}

顺序表插入新元素

int AppendSeqlist(Seqlist *pseqlist,DataType data)
{if(IsFullSeqlist(pseqlist))//判断是否存满,存满则返回-1{return -1;}pseqlist->data[pseqlist->clen]=data;pseqlist->clen++;return 0;
}

在指定位置插入新元素

int PosInsertSeqList(Seqlist *pseqlist,int Pos,DataType data)
{int n=0;if(IsFullSeqlist(pseqlist)){return -1;}for(n=pseqlist->clen;n>Pos;n--)//将指定位置以后的元素都向后挪{pseqlist->data[n]=pseqlist->data[n-1];}pseqlist->data[Pos]=data;pseqlist->clen++;return 0;}

遍历顺序表所有元素,对每个元素完成指定操作

//pFun是函数指针,指向一个要对元素做什么操作的函数
int ForeachSeqList(Seqlist *pseqlist,int (*pFun)(void *Element,void *arg),void *arg)
{int i=0;int ret=0;for(i=0;i<pseqlist->clen;i++){ret=pFun(&pseqlist->data[i],arg);if(ret!=0){return -1;}}return 0;
}

      (指定操作1)  打印所有元素

int Print(void *Element,void *arg)
{int *pData=Element;printf("%d  ",*pData);return 0;
}

调用语句

printf("元素内容:\n");
    ForeachSeqList(seqlist,Print,NULL); 

        (指定操作2)更新指定元素

        (这里将4更新为60)(也可以用arg传参)

int UpdateFun(void *Element,void *arg)
{int *pData=Element;if(*pData==4){*pData=60;}return 0;
}

调用语句

  printf("修改元素为4为60\n");
    ForeachSeqList(seqlist,UpdateFun,NULL);

        (指定操作3)找到指定元素的位置   

(这里找元素10)  (也可以用arg传参)

int FindFun(void *Element,void *arg)
{int *pData=Element;if(*pData==10){return 1;}else{(*(int *)arg)++;printf("(*(int *)arg)=%d\n",(*(int *)arg));}return 0;
}

调用语句

  printf("查找元素10的位置\n");
    ForeachSeqList(seqlist,FindFun,&n);

//当找到元素后,FindFun返回1,ForeachSeqlist函数检测到返回值不为0,跳出while循环

删除指定位置的元素

int DeleteSeqList(Seqlist *pseqlist,int Pos)
{int i=0;if(EmptySeqlist(pseqlist))//判断是否为空,为空返回-1{return -1;}if(!(Pos>=0&&Pos<pseqlist->clen))//判断位置是否合理{return -2;}for(i=Pos;i<pseqlist->clen;i++)//将该位置后面的元素都往前挪{pseqlist->data[i]=pseqlist->data[i+1];}pseqlist->clen--;
}

清零顺序表

int ClearSeqList(Seqlist *pseqlist)
{pseqlist->clen=0;return 0;
}

销毁顺序表

int DestorySeqList(Seqlist **pseqlist)
{free((*pseqlist)->data);free((*pseqlist));*pseqlist=NULL;return 0;
}


http://www.mrgr.cn/news/12452.html

相关文章:

  • SpringCloud整合Nacos
  • [图论]游戏
  • 《陈天奇:机器学习科研的十年》阅读笔记
  • 金融基础知识-银行间债券市场交易规则+场外市场交易规则
  • 云计算day32
  • 力扣面试150 插入区间 模拟
  • 【Java项目开发】点菜系统(无前端)
  • 【扩散模型(八)】Stable Diffusion 3 diffusers 源码详解2 - DiT 与 MMDiT 相关代码(下)
  • 重卡智能充电机器人
  • while
  • windows11 开发环境资源整理
  • 命令模式详解
  • PPT布局图片文本解析检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • 智慧升级,触手可及:Vatee万腾平台的全方位服务
  • 国内使用tensorflow_datasets加载数据
  • STM32—USART串口外设
  • 数据结构与算法——Java实现 2.衡量算法好坏的标准
  • ETAS工具链自动化实战指南<二>
  • 【STM32 Blue Pill编程】-UART数据接收与发送(轮询模式)
  • Centos 设置成中国时区