您的位置:首页 > 娱乐 > 明星 > 数据结构:顺序表

数据结构:顺序表

2024/10/7 4:35:02 来源:https://blog.csdn.net/m0_64378221/article/details/141611139  浏览:    关键词:数据结构:顺序表

目录

结构体

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

 顺序表结构体

函数 

初始化顺序表

判断顺序表是否存满

判断顺序表是否为空

顺序表插入新元素

在指定位置插入新元素

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

      (指定操作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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com