You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
832 B

2 years ago
//顺序访问所有元素
for(i=0;i<L.length;i++)
visit(L.elem[i]);
//查找元素 x
for(i=0;i<L.length;i++)
if(L.elem[i]==x) break;
if(i<L.length)
else
//插入算法
bool ListInsert(SqList L,int i,DataType x)
{
if(L.length==MAXSIZE || i<1||i>L.length+1)
return false;//失败
//元素后移
for(L.length-1;j>=i-1;j--)//这里j为下标从L.length-1到i-1
L.length[j+1]=L.length[j];
//插入x
L.length[i-1]=x;
//表长增1
L.length++;
return true;//插入成功
}//时间复杂度 n/2
//删除算法
bool ListDelete(SqList& L,int i,DataType&x)
{
if(L.length==0||i<1||i>L.length)
return flase;//失败
x=L.elem[i-1];
for(j=i;j<L.length;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return true;//删除成功
}//时间复杂度 n-1/2