沒時間搞排版直接上代碼。
#include?<stdio.h>
#include?<stdlib.h>
#define?maxsize?100
typedef?struct{
??int?data[maxsize];
??int?length;
}Sqlist;
int?findElem_cmp(Sqlist?L,int?x)
{
??int?i;
??for(i=0;i<L.length;++i)
??{
????if?(L.data[i]>x)
????{
??????return?i;
????}
??}
??return?i;
}
void?insertElem_cmp(Sqlist?*L,int?x)
{
??int?local=0;
??local=findElem_cmp(*L,x);
??for(int?i=L->length-1;i>=local;--i)
??{
????(*L).data[i+1]=(*L).data[i];
??}
???(*L).data[local]=x;
???++(*L).length;
}
int?findElem_byVal(Sqlist?L,int?e)
{
??for(int?i=0;i<L.length;i++)
??{
????if(L.data[i]==e)
????{
??????return?i;
????}
??}
??return?-1;
}
void?intial(Sqlist?*L)
{
??L->length=8;
??int?a[8]={4,5,7,9,10,13,17,21};
??for(int?i=0;i<L->length;i++)
????L->data[i]=a[i];
????printf("begin:");
??for(int?i=0;i<8;i++)
????printf("%4d",L->data[i]);???
}
int?insertElem_bylocal(Sqlist?*L,int?p,int?e)
{
??if(p<0?||?p>L->length-1?||?p==maxsize)
????return?0;
??for(int?i=L->length-1;i>=p;--i)
??{
????L->data[i+1]=L->data[i];
??}
??L->data[p]=e;
??++(L->length);
??return?1;
}
int?deleteElem_byP(Sqlist?*L,int?p,int?*e){
??if(p<0?||?p>=L->length-1)
????return?0;
??*e=L->data[p];
????for(int?i=p;i<L->length-1;i++)
????{
??????L->data[i]=L->data[i+1];
????}
????--(L->length);
????return?1;
}
int?getelm_byP(Sqlist?*L,int?p,int?*e)
{
??if(p>0?&&?p>L->length-1)
????return?0;
??*e=L->data[p];
??return?1;
}
int?main()
{
??Sqlist?L;
??//初始化
??intial(&L);
??//查看元素可插入位置
??int?num=findElem_cmp(L,12);
??printf("\nlocal=?%d",num+1);
??//插入元素后排列
??printf("\nafter?inert:");
??insertElem_cmp(&L,12);
??for(int?i=0;i<L.length;i++)
????printf("%4d",L.data[i]);?
??//比較參數(shù)返回相同的下表
??if(findElem_byVal(L,12)!=-1)
??printf("\n12?local:%d",findElem_byVal(L,12)+1);
??else
??{
????printf("\nerror");
??}
??//按位置插入數(shù)據(jù)
??printf("\n?new?line:");
??if(insertElem_bylocal(&L,8,88)==1)
??{
????for(int?i=0;i<L.length;i++)
????printf("%4d",L.data[i]);?
??}else{
?????printf("\nerror");
??}
??//按位置刪除元素
??int?e;
??printf("\n?new?line:");
??if(deleteElem_byP(&L,8,&e)==1)
??{
????for(int?i=0;i<L.length;i++)
??????printf("%4d",L.data[i]);?
??}
??printf("\n?delete?elm:%d\n",e);
??//按位置返回元素
??if(getelm_byP(&L,7,&e)==1)
????printf("\n?the?elm:%d\n",e);
???else
?????printf("\nerror");
??system("pause");?
???return?0;
}