王道p18 2.设计一个高效算法,将顺序表L的所有元素逆置,要求算法的空间复杂度为 O(1)。(c语言代码实现)

 视频讲解在这(支持一下吧,谢谢各位大佬):

c语言代码实现数据结构课后代码题顺序表p18 2_哔哩哔哩_bilibili

本题代码如下

void nizhi(struct sqlist* s)
{
	int temp = 0;
	for (int i = 0; i < s->length / 2; i++)
	{
		temp = s->a[i];
		s->a[i] = s->a[s->length - i - 1];
		s->a[s->length - 1 - i] = temp;
	}
}

完整测试代码

#include
#define Max 50
struct sqlist
{
	int a[Max];
	int length;
};
void nizhi(struct sqlist* s)
{
	int temp = 0;
	for (int i = 0; i < s->length / 2; i++)
	{
		temp = s->a[i];
		s->a[i] = s->a[s->length - i - 1];
		s->a[s->length - 1 - i] = temp;
	}
}
int main()
{
	struct sqlist s;
	int j = 0;
	s.length = 5;
	for (j = 0; j < s.length; j++)
		scanf("%d", &s.a[j]);
	printf("原先数组为:");
	for (j = 0; j < s.length; j++)
		printf("%d", s.a[j]);
	nizhi(&s);
	printf("\n逆置后的数组为:");
	for (j = 0; j < s.length; j++)
		printf("%d", s.a[j]);
	return 0;
}

你可能感兴趣的:(顺序表,算法,c语言,数据结构)