user-0 提问: 顺序查找 哨兵

回复:
user: user-1
content: 第一个元素是哨兵的位置，所以20是无效元素

user: user-0
content: 引用 5 楼 zhangxiangDavaid 的回复:[quote=引用 4 楼 qq_20242109 的回复:] [quote=引用 1 楼 zhangxiangDavaid 的回复:] 忽略作解释：  int main() { 	//以search_test的写法可知，第一个元素被当做哨兵处理，所以有效元素是3个，不是4个，故count=3; 	int list_data[] = { 20, 45, 77, 74 };  	//int count = 4; 	int count = 3; 	int key = 201; 	int val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 2; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 77; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	return 0; } int seach_test(int *list_data, int count, int key) { 	int i = 0; 	list_data[0] = key;  //设置第一个元素，实质成为哨兵 	i = count; 	while (list_data[i] != key) { 		i--; 	} 	return i; } //key=201,val=2 //key=2,val=0 //key=77,val=2    假如key为20，返回值为0，跟key为201的返回值是一样的[/quote] 对，返回0就是表明查找失败[/quote] 20在数组中是存在的，返回0是20在数组中的下标，不是失败吧

user: user-1
content: 引用 4 楼 qq_20242109 的回复:[quote=引用 1 楼 zhangxiangDavaid 的回复:] 忽略作解释：  int main() { 	//以search_test的写法可知，第一个元素被当做哨兵处理，所以有效元素是3个，不是4个，故count=3; 	int list_data[] = { 20, 45, 77, 74 };  	//int count = 4; 	int count = 3; 	int key = 201; 	int val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 2; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 77; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	return 0; } int seach_test(int *list_data, int count, int key) { 	int i = 0; 	list_data[0] = key;  //设置第一个元素，实质成为哨兵 	i = count; 	while (list_data[i] != key) { 		i--; 	} 	return i; } //key=201,val=2 //key=2,val=0 //key=77,val=2    假如key为20，返回值为0，跟key为201的返回值是一样的[/quote] 对，返回0就是表明查找失败

user: user-0
content: 引用 1 楼 zhangxiangDavaid 的回复:忽略作解释：  int main() { 	//以search_test的写法可知，第一个元素被当做哨兵处理，所以有效元素是3个，不是4个，故count=3; 	int list_data[] = { 20, 45, 77, 74 };  	//int count = 4; 	int count = 3; 	int key = 201; 	int val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 2; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 77; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	return 0; } int seach_test(int *list_data, int count, int key) { 	int i = 0; 	list_data[0] = key;  //设置第一个元素，实质成为哨兵 	i = count; 	while (list_data[i] != key) { 		i--; 	} 	return i; } //key=201,val=2 //key=2,val=0 //key=77,val=2    假如key为20，返回值为0，跟key为201的返回值是一样的

user: user-1
content: key=201,val=0

user: user-1
content: 略作解释

user: user-1
content: 忽略作解释：  int main() { 	//以search_test的写法可知，第一个元素被当做哨兵处理，所以有效元素是3个，不是4个，故count=3; 	int list_data[] = { 20, 45, 77, 74 };  	//int count = 4; 	int count = 3; 	int key = 201; 	int val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 2; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	key = 77; 	val = seach_test(list_data, count, key); 	printf("key=%d,val=%d\n", key, val); 	return 0; } int seach_test(int *list_data, int count, int key) { 	int i = 0; 	list_data[0] = key;  //设置第一个元素，实质成为哨兵 	i = count; 	while (list_data[i] != key) { 		i--; 	} 	return i; } //key=201,val=2 //key=2,val=0 //key=77,val=2