1.顺序表按元素删除
2.顺序表按元素插入其后继位置
3.顺序表排序
4.顺序表合并
5.顺序表空间释放
//顺序表按元素删除
int delete_data(seqlist list,datatype e)
{
int flag = search_data(list,e);
if(flag == -1)
{
printf("删除失败\n");
return -1;
}
delete_sub(list,flag);
return 0;
}
//顺序表按元素插入其后继位置
int insert_after_sub(seqlist list,datatype e, datatype key)
{
int flag = search_data(list,e);
//元素不存在,或满
if(flag == -1|| full_seqlist(list))
{
printf("插入失败\n");
return -1;
}
//调用按下标插入
insert_sub(list,flag+1,key);
return 0;
}
//顺序表排序
int sort(seqlist list)
{
if(list == NULL || empty_seqlist(list))
{
printf("排序失败\n");
return -1;
}
//冒泡排序
for(int i=0;i < list->len-1;i++)
{
int count = 0;
for(int j=0;j<list->len-1-i;j++)
{
if(list->data[j] > list->data[j+1])
{
datatype temp = list->data[j];
list->data[j] = list->data[j+1];
list->data[j+1] = temp;
count++;
}
}
if(count == 0)
{
break;
}
}
return 0;
}
//顺序表空间释放
seqlist free_space(seqlist list)
{
if(list == NULL)
{
return NULL;
}
free(list);
return list;
}
//顺序表合并
void combine(Seqlist *la,Seqlist *lb,Seqlist *lc)
{
int p = 0,q = 0, r = 0;
while(p< la->len && q<lb->len)
{
if(la->data[p] <= lb->data[q])
{
lc->data[r] = la->data[p];
r++;p++;
lc->len++;
}
else
{
lc->data[r] = lb->data[q];
r++;q++;
lc->len++;
}
}
while(p < la->len) //la剩余元素放入lc中
{
lc->data[r] = la->data[p];
r++;p++;
lc->len++;
}
while(q< lb->len) //lb剩余元素放入lc
{
lc->data[r] = lb->data[q];
r++;q++;
lc->len++;
}
}