以下是用C语言编写的可运行代码实现三种常用的数据结构查找算法:

一、折半查找算法:

#include <stdio.h>

int binarySearch(int arr[], int n, int target) {
    int left = 0;
    int right = n - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int target = 9;
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, n, target);
    if (result == -1) {
        printf("查找失败\n");
    } else {
        printf("查找成功,目标元素在有序表的第 %d 个位置\n", result + 1);
    }
    return 0;
}

二、二叉排序树查找:

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
} TreeNode;

TreeNode* insert(TreeNode* root, int value) {
    if (root == NULL) {
        TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
        newNode->data = value;
        newNode->left = NULL;
        newNode->right = NULL;
        return newNode;
    }
    if (value < root->data) {
        root->left = insert(root->left, value);
    } else if (value > root->data) {
        root->right = insert(root->right, value);
    }
    return root;
}

TreeNode* search(TreeNode* root, int target) {
    if (root == NULL || root->data == target) {
        return root;
    }
    if (target < root->data) {
        return search(root->left, target);
    } else {
        return search(root->right, target);
    }
}

int main() {
    int arr[] = {7, 3, 9, 1, 5, 11, 13, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    TreeNode* root = NULL;
    for (int i = 0; i < n; i++) {
        root = insert(root, arr[i]);
    }
    int target = 9;
    TreeNode* result = search(root, target);
    if (result == NULL) {
        printf("查找失败\n");
    } else {
        printf("查找成功,目标元素在二叉排序树中\n");
    }
    return 0;
}

三、哈希表查找:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10

typedef struct HashNode {
    char name[20];
    struct HashNode* next;
} HashNode;

int hash(char* name) {
    int sum = 0;
    for (int i = 0; i < strlen(name); i++) {
        sum += name[i];
    }
    return sum % SIZE;
}

void insert(HashNode* hashTable[], char* name) {
    int index = hash(name);
    HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
    strcpy(newNode->name, name);
    newNode->next = NULL;
    if (hashTable[index] == NULL) {
        hashTable[index] = newNode;
    } else {
        HashNode* temp = hashTable[index];
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

int search(HashNode* hashTable[], char* name) {
    int index = hash(name);
    HashNode* temp = hashTable[index];
    while (temp != NULL) {
        if (strcmp(temp->name, name) == 0) {
            return 1;
        }
        temp = temp->next;
    }
    return 0;
}

int main() {
    HashNode* hashTable[SIZE];
    for (int i = 0; i < SIZE; i++) {
        hashTable[i] = NULL;
    }
    insert(hashTable, "Alice");
    insert(hashTable, "Bob");
    insert(hashTable, "Charlie");
    char name[20];
    printf("请输入要查找的人名:");
    scanf("%s", name);
    if (search(hashTable, name)) {
        printf("查找成功,人名在哈希表中\n");
    } else {
        printf("查找失败,人名不在哈希表中\n");
    }
    return 0;
}

以上代码分别实现了折半查找算法、二叉排序树查找和哈希表查找的功能。

C语言实现数据结构查找算法:折半查找、二叉排序树查找和哈希表查找

原文地址: https://www.cveoy.top/t/topic/f3yO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录