| 10 | |
| 11 | |
| 12 | bool ListInsert(SqList &L, int i, ElemType e){ |
| 13 | |
| 14 | // i非法 i=1 表头 i=L.length+1 表尾巴 |
| 15 | if(i<1||i>L.length+1){ |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | // 存储空间满,无法插入 |
| 20 | if(L.length >= MaxSize){ |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | // 遍历,将位置元素往后移动,注意从后往前循环,避免值被覆盖 |
| 25 | for(int j=L.length; j>=i;j--){ |
| 26 | L.data[j]=L.data[j-1]; |
| 27 | } |
| 28 | |
| 29 | // 此时,表L中的第i个元素和第i+1元素素值一样,将新元素存入i位置即可 |
| 30 | |
| 31 | // 第i个元素,对应的位置角标为i-1 |
| 32 | L.data[i-1]=e; |
| 33 | |
| 34 | // 表长度加1 |
| 35 | L.length++; |
| 36 | |
| 37 | // 返回插入成功 |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | bool ListDelete(SqList &L, int i, ElemType &e){ |
nothing calls this directly
no outgoing calls
no test coverage detected