【数据结构】顺序表的插入和删除

#define MaxSize 10
typedef struct
{
	int data[MaxSize];
	int length;
}SqList;
bool ListInsert(SqList &L,int i,int e)
{
	if (i > 1 || i > L.length + 1)
	{
		return false;
	}
	if (L.length >= MaxSize)
		return false;
	for (int j= L.length;j>=i;j--)//将第i个元素及之后的元素后移
	{
		L.data[j] = L.data[j - 1];
	}
	L.data[i - 1] = e;//在位置i加入e
	L.length++;//长度加1
}


bool ListDelte(SqList& L,int i, int &e)
{
	if (i > 1 || i > L.length + 1)
	{
		return false;
	}
	e = L.data[i - 1];
	for (int  j = i; j < L.length; j++)
	{
		L.data[j - 1] = L.data[j];
	}
	L.length--;
	return true;
}

 

猜你喜欢

转载自blog.csdn.net/weixin_46472622/article/details/126607005
今日推荐