参考 数据结构(c语言描述)(第二版)徐孝凯 编著
自定义头文件
头文件名:List.h
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
typedef int ElemType;
typedef struct SequList //定义顺序储存线性表的结构类型
{
ElemType* list; //只想动态数组空间的指针
int len; //保存线性表的当前长度
int MaxSize; //保存List数组的长度
}List;
#endif
线性表在顺序存储下运算的算法
/*/
//初始化L为一个空表,其储存数组的长度为ms
//
void initList(List* L,int ms)
{
//初始化线性表,分配动态储存和置为空表
if(ms<10)
{
L->MaxSize=10;//若ms的值小于10,则设置是数组长度为10
}
else
{
L->MaxSize=ms;//若ms的值大于10,则设置其为ms的值
}
L->list=calloc(L->MaxSize,sizeof(ElemType));//动态分配线性表空间
if(!L->list)//分配空间失败,退出
{
printf("分配空间失败!\n");
exit(1);
}
L->len=0;//初始化把线性表置为空
}
/**********************************************************************/
//得到线性表中第pos的值
/*********************************************************************/
int geList(List* L,ElemType* item,int pos)
//通过item带回第pos的值并返回1,否则返回0
{
if(pos<1||pos>L->len)//pos超出数组长度
{
return 0;
}
else
{
*item=L->list[pos-1];
return 1;
}
}
/***********************************************************************/
//从线性表中查找与item值相匹配的第一个元素并有参数带回
int findlist(List* L,ElemType* item)
{
L->list[L->len]=*item;//将待查找的值存入线性表表尾作为岗哨
int i=0;
while(*item!=L->list[i])
{
i++;
}
if(i<L->len)
{
return 1;
}
else
{
return 0;
}
}
/*********************************************************************/
//修改线性表中与item值相匹配的第一个元素值为modify
int modifyList(List* L,ElemType item,ElemType modify)
{