順序查找(34)

源代碼:

//
//  main.c
//  Sxsearch
//  程序目的:在r[n]數組中順序查找關鍵字等於kx的記錄
//  Created by zhen7216 on 2016/12/4.
//  Copyright © 2016年 chenzhen. All rights reserved.
//

#include <stdio.h>
#define max 7

int searchList[max] = {0, 13, 25, 16, 23, 57, 66};

//在r[n]數組中查找
int sxSearch(int r[], int n, int kx) {
    int i;
    r[0] = kx;//在下標爲0處設置監視哨
    
    i = n - 1;
    while(r[i] != kx)
        i--;
    return i;//返回查找的位置
}

int main() {
    int key;
    printf("please input the data you want to search:\n");
    scanf("%d", &key);
    
    if (sxSearch(searchList, max, key) != 0)
        printf("the data is found\n");
    else
        printf("the data is not found\n");
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章