就是对数组结构的操作
1 typedef struct LNode *List; 2 struct LNode { 3 ElementType Data[MaxSize]; 4 int Last; 5 }; 6 struct LNode L; 7 List PtrL; 8 9 //建立空表 10 List MakeEmpty() { 11 List PtrL; 12 PtrL = (List)malloc( sizeof(struct LNode) ); 13 PtrL->Last = -1; 14 return PtrL; 15 } 16 17 //查找 18 int Find( ElementType X, List PtrL ) { 19 int i = 0; 20 while( i <= PtrL->Last && PtrL->Data[i]!= X ) 21 i++; 22 if(i > PtrL->Last) return -1; //没找到 23 else return i; 24 } 25 26 //插入 27 void Insert( ElementType X, int i, List PtrL ) { 28 int j; 29 if( PtrL->Last == MaxSize-1 ) { 30 printf("表满"); 31 return; 32 } 33 if( i < 1 || i > PtrL->Last+2 ) { 34 printf("位置不合法"); 35 return; 36 } 37 for( j = PtrL->Last; j>=i-1; j-- ) 38 PtrL->Data[j+1] = PtrL->Data[j]; //先把i后面的元素往后移 39 PtrL->Data[i-1] = X; 40 PtrL->Last++; 41 } 42 43 //删除 44 void Delete( int i, List PtrL ) { 45 int j; 46 if( i < 1 || i > PtrL->Last+1 ) { 47 printf("不存在第%d个元素",i); 48 return; 49 } 50 for( j=i; j<=PtrL->Last; j++ ) 51 PtrL->Data[j-1] = PtrL->Data[j]; // 把i 后面的元素往前移 52 PtrL->Last--; 53 }
原文:https://www.cnblogs.com/Pio-GD/p/14379376.html