考研数据结构C语言代码实现——线性表/顺序表的插入操作

#include 
#include
#include
#include

#define MaxSize 10
struct SqList{
	int data[MaxSize];
	int length;
};
//i表示在第几位插入,不是数组下标
bool ListInsert(struct SqList &L,int i,int val){
	//判断插入位置是否合理
	if(i<1 || i>L.length+1)
		return false;
	if(L.length >= MaxSize)
		return false;
		
	//最好时间复杂度为O(1)
	//最坏时间复杂度为O(n),n为L.length
	//平均时间复杂度为O(n)->其实是n+1/2
	for(int j = L.length;j>=i;j--)
		L.data[j]=L.data[j-1];
	L.data[i-1]=val;
	L.length++;
	return true;
}
int main(void)
{
  	SqList L;
	ListInsert(L,1,10);
	ListInsert(L,2,10);
	printf("Yes");
	return 0;
}

你可能感兴趣的:(其他学习,数据结构)