线性表是具有相同属性的数据元素的一个有限序列。
线性表的存储结构有顺序、链接、索引、散列等多种方式。
以下代码展现的是顺序存储下线性表的操作实现
类型定义:
struct List{
ElemType list[MaxSize];
int size;
};
初始化:
void InitList(struct List *L,int ms)
{
if(ms<=0){printf("ms not valid\n");exit(1);}
L->MaxSize=ms;
L->list=malloc(ms*sizeof(ElemType));
if(!L->List){
printf("malloc failed!\n");
exit(1);
}
L->size=0;
}
清除线性表:(清除所有元素,释放动态分配的存储空间,使之成为一个空表)
void ClearList(struct List *L)
{
if(L->list!=NULL){
free(L->list);
L->list=0;
L->size=L->MaxSize=0;
}
}
返回线性表的长度
int SizeList(struct List *L)
{
return L->size;
}
判断线性表是否为空
int EmptyList(struct List *L)
{
if(L->size==0)
return 1;
else
return 0;
}
返回线性表L中第pos个元素的值,若pos超出范围,则停止程序的运行
ElemType GetElem(struct List *L,int pos)
{
if(pos<1||pos>L->size)
{
printf("pos is out of boundary!\n");
exit(1);
}
return L->list[pos-1];
}
顺序扫描输出线性表L中的每个元素
void TraverseList(struct List *L)
{
int i;
for(i=0;i<L->size;i++)
printf("%d",L->list[i]);
printf("\n");
}
从线性表L中查找值与x相等的元素,若查找成功则返回其位置,否则返回-1
int FindList(struct List *L,ElemType x)
{
int i;
for(i=0;i<L->size;i++)
{
if(x==L->list[i])
return i+1;
}
return -1;
}
把线性表L中第pos个元素的值修改为x的值,若修改成功返回1,否则返回0
int UpdatePosList(struct List *L,int pos,ElemType x)
{
if(pos<1||pos>L->size)
return 0;
L->list[pos-1]=x;
return 1;
}
向线性表L的表头插入元素x
void InsertFirstList(struct List *L,ElemType x)
{
int i;
if(L->size==MaxSize)
againMalloc(L);
for(i=L->size-1;i>=0;i--)
L->list[i+1]=L->list[i];
L->list[0]=x;
L->size++;
}
void againMalloc(struct List *L)
{
ElemType *p=realloc(L->list,2*L->MaxSize*sizeof(ElemType));
if(!p){
printf("remalloc failed!\n")
exit(1);
}
free(L->list);
L->list=p;
}
向线性表L的表尾插入元素x
void InsertLastList(struct List *L,ElemType x)
{
if(L->size==MaxSize)
againMalloc(L);
L->list[L->size]=x;
L->size++;
}
向线性表L中的第pos个位置插入元素x,若插入成功返回1,否则返回0
int InsertPosList(struct List *L,int pos,ElemType x)
{
int i;
if(pos<1||pos>L->size)
return 0;
if(L->size==MaxSize)
againMalloc(L);
for(i=L->size;i>=pos;i--)
L->list[i]=L->list[i-1];
L->list[pos-1]=x;
L->size++;
return 1;
}
向有序线性表L中插入元素x,使得插入后仍然有序
void InsertOrderList(struct List *L,ElemType x)
{
int i,j;
if(L->size==MaxSize)
againMalloc(L);
for(i=0;i<L->size;i++)
if(L->list[i]>x)
break;
for(j=L->size-1;j>=i;j--)
L->list[j+1]=L->list[j];
L->list[i]=x;
L->size++;
}
向线性表中删除表头元素并返回它,若删除失败则停止程序运行
ElemType DeleteFirstList(struct List *L)
{
ElemType temp;
int i;
if(L->size==0){
printf("List is empty,cann't not delete!\n");
exit(1);
}
temp=L->list[0];
for(i=0;i<L->size-1;i++)
L->list[i]=L->list[i+1];
L->size--;
return temp;
}
向线性表L中删除表尾元素并返回它,若删除失败则停止程序运行
ElemType DeleteLastList(struct List *L)
{
if(L->size==0){
printf("List is empty,cann't not delete!\n");
exit(1);
}
L->size--;
return L->list[L->size];
}
从线性表L中删除第pos个元素并返回它,若删除失败则停止运行程序
ElemType DeletePosList(struct List *L)
{
ElemType temp;
int i;
if(pos<1||pos>L->size)
exit(1);
temp=L->list[pos-1];
for(i=pos;i<L->size;i++)
L->list[i-1]=L->list[i];
L->size--;
return temp;
}
从线性表L中删除值为x的第一个元素,若删除成功返回1否则返回0
int DeleteValueList(struct List *L)
{
int i,j;
for(i=0;i<L->size;i++)
if(L->list[i]==x) break;
if(i==L->size)return 0;
for(j=i+1;j<L->size;j++)
L->List[j-1]=L->List[j];
L->size--;
return 1;
}