1 #include<stdio.h> 2 #define MaxSize 100 3 typedef int Elemtype; 4 typedef struct{ 5 Elemtype data[MaxSize]; 6 int length; 7 }SqList; 8 9 /*插入元素操作*/ 10 bool ListInsert(SqList &l,int i,Elemtype e){ 11 if(i<1||i>l.length+1) 12 return false; 13 if(l.length>=MaxSize) 14 return false; 15 for(int j=l.length;j>i;j--){ 16 l.data[j-1]=l.data[j]; 17 } 18 l.data[i-1]=e; 19 l.length++; 20 21 return true; 22 }
23 24 /*刪除元素操作*/ 25 bool ListDelete(SqList &l,int i,ElemType &e){ 26 if(i<1||i>l.length) 27 return false; 28 e = l.data[i-1]; 29 for(int j = i;j<l.length;j++){ 30 l.data[j-1] = l.data[j]; 31 } 32 return true; 33 }
1 /*测试代码*/ 2 int main(){ 3 SqList l; 4 Elemtype e; 5 l.length=0; 6 ListInsert(l,1,10); 7 ListInsert(l,1,20); 8 ListDelete(l,1,e); 9 printf("%d\n",e); 10 printf("%d\n",l.length); 11 return 0; 12 }
原文:https://www.cnblogs.com/huangpeideng/p/10167976.html