//// Created by ~风轻云淡~ on 2023/1/10.//#ifndefPRO01_DATAELEMENT_H#definePRO01_DATAELEMENT_H#defineMAX_SIZE255//1.定义数据元素//定义数据元素的方法: 例如:data={{1,""},{2,""},{3,""}}//typedef int ElementType;typedefstruct{int id;char* name;}ElementType;//2.定义顺序表结构typedefstruct{
ElementType datas[MAX_SIZE];//顺序表中的元素集合int length;//当前顺序表中的元素个数}SeqList;#endif//PRO01_DATAELEMENT_H
//// Created by ~风轻云淡~ on 2023/1/10. 定义顺序表//#ifndefPRO01_SEQUENCELIST_H#definePRO01_SEQUENCELIST_H#include"stdio.h"#include"stdlib.h"//导入顺序表#include"DataElement.h"//1.初始化链表 seqList要初始化的顺序表//2.elementArray 初始化时 要天津爱的元素内容数组//3.length 初始化时 天津爱的元素个数voidInitList(SeqList *seqList,ElementType *elementArray,int length);//1.向顺序表中的index下标出插入某个元素//seqList//index 要插入的下标//element 要插入的元素voidInsertElement(SeqList *squList,int index,ElementType elementType);voidPrintList(SeqList*seqList);#endif//PRO01_SEQUENCELIST_H
//// Created by ~风轻云淡~ on 2023/1/10.//#include"SequenceList.h"//1.初始化链表 seqList要初始化的顺序表//2.elementArray 初始化时 要天津爱的元素内容数组//3.length 初始化时 天津爱的元素个数voidInitList(SeqList *seqList,ElementType *elementArray,int length){// 下标为i及下标为i以后的所有数据元素后移//下标i的位置if(length>MAX_SIZE){printf("超出了数组的最大边界");return;}for(int i=0;i<length-1;i++){// 每个循环都插在下标为i的位置入一个元素InsertElement(seqList,i,elementArray[i]);}}//1.向顺序表中的index下标出插入某个元素//seqList//index 要插入的下标//element 要插入的元素voidInsertElement(SeqList *squList,int index,ElementType elementType){//1.验证插入后的元素空间是否超过最大空间//2.index的值是否合法 [0,MAX_SIZE-1]//3.插入的index应该在length之内//4.从第length-1个下标开始 前面一个元素赋值给后面一个元素 将插入的元素赋值给下标为index元素if(squList->length+1>=MAX_SIZE){printf("数组已经满 插入元素越界\n");return;}if(index<0||index>MAX_SIZE-1){printf("只能在允许的范围内插入元素[0,%d]",MAX_SIZE);return;}if(index>squList->length){printf("插入的下标超过了数组的最大长度 -1 插入失败\n");return;}//开始插入算法for(int i = squList->length-1; i >=index ; i--){
squList->datas[i+1]=squList->datas[i];}//5.将要插入的值赋值给index个元素
squList->datas[index]=elementType;// 顺序表的总长度加以
squList->length++;}voidPrintList(SeqList*seqList){for(int i =0; i <seqList->length ;++i){printf("%d\t%s\n",seqList->datas[i].id,seqList->datas[i].name);}}