首页 > 试题广场 >

已知线性表(a1,a2, … ,an )以顺序存储结构为存储

[问答题]

已知线性表(a1,a2, ,an )以顺序存储结构为存储结构,其类型定义如下:

#define  LIST_INIT_SIZE  100   // 顺序表初始分配容量

typedef  struct  {

Elemtype   *elem;        // 顺序存储空间基址

int  length;              // 当前长度(存储元素个数)

}SqList;

设计一个算法,删除其元素值为 x 的结点(假若 x 是唯一的) 。并求出其算法的平均时间复杂度。其算法函数头部如下:

S tatus ListDelete(Sqlist &L,Elemtype x)

{

……

}

Status  ListDelete(Sqlist &L,ElemType x)

{

int i,j;

for(i=0;i<L->length;i++)

if(L->elem[i]==x) break;

if(i=L->length) return ERROR;

for(j=i;j<L->lengthi-1;j++)

L->elem[j]=L->elem[j+1];

L->length--;

} 8 分)

平均时间复杂度:( 2 分)

设元素个数记为 n ,则平均时间复杂度为:

发表于 2017-05-14 22:10:32 回复(0)