顺序查找
#include <stdio.h>
typedef void (*call_back)(int);
void printIndex(int index)
{
printf("%d\t",index);
}
int linear(int *array, int length, int target, call_back ptr)
{
int count = 0;
for (int i = 0; i < length; i++)
{
if(array[i] == target)
{
ptr(i);
count++;
}
}
return count;
}
int main()
{
int linear_array[20] = {234, 12, 78, 980, 345, 2, 17, 78, 643, 108, 23, 78, 256, 90, 379, 23, 98, 100, 21, 8};
int target = 78;
int count = linear(linear_array, sizeof(linear_array)/sizeof(int), target, printIndex);
if(count == 0)
{
printf(" \nThere is no num in the array matching the target. \n");
}
else
{
printf("\nThere are %d nums matching the target num and the positions are above", count);
}
}
2 7 11
There are 3 nums matching the target num and the positions are above