数据结构课件第二章线性表2讲.pdfVIP

  • 0
  • 0
  • 约7.05千字
  • 约 13页
  • 2023-09-28 发布于北京
  • 举报
第二章 线性表 第 2 讲 2.2 线性表的顺序表示 (2) 1、线性表 2、线性表删除 3、线性表定位 4、线性表的缺点 2021/9/27 1/13 – • 定义:线性表的 是指在第i (1i  n+1)个元 前 一个新的数据元素x ,使长度为n的线性表 (a , a , a , a , a ) …… 1 2 , …… i − 1 i n 变成长度为n+1的线性表 (a , a ……, a , x , a , a ) …… 1 2 , i − 1 i n 需将第n至第i共 (n-i+1)个元素后移 顺序表 演示 此算法的时间复杂度为: O( ListLength(L) ) 2021/9/27 2/13 Status ListInsert_Sq(SqList L,int i,ElemType e) {//在顺序线性表L 中第i个位置之前 新的元素e, //i的合法值为1≤i≤ListLength_Sq(L)+1 if (i1||iL.length+1) return ERROR;//i值不合法 if (L.length=L.listsize){//当前 空间已满,增加分配 newbase=(ElemType *) realloc (L.elem, (L.listsize+LIST REMENT)*sizeof(ElemType)); if (!newbase) exit (OVERFLOW); // 分配失败 L.elem=newbase; //新基址 L.listsize+=LIST REMENT; //增加 容量 } q=(L.elem[i-1]); //q为 位置 for (p=(L.elem[L.length-1]);p=q;--p) *(p+1)=*p; // 位置及 的元素右移 *q=e; // e ++L.length; //表长增1 return OK; 202 1/9/27 3/13 }//ListInsert_Sq C源代码: int sxbcr(int i,int x,int v[],int *p) { int j,n; n=*p; if((

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档