データを端から順番にスキャンしながらキーの比較を行う探索法
struct{
int key;
int data;
}table[100]
int n;
int search(int key)
{
int i = 0;
while(i < n){
if(table[i].key == key){
return (table[i].data);
}
i++;
}
return -1;
}
keyの終端のキー値を探索するキーにすることで、探索を高速にする。 探索するキーが最後と終端な場合は見つからなかったとして、-1を返す。
int search(int key)
{
int i = 0;
table[99].key = key
while(1){
if(table[i].key == key){
return (i == 99 ? -1 : table[i].data);
}
i++;
}
}
参考文献