c语言实验报告

编写C语言代码分别解答以下问题:(1)自动生成包含数百、数千、数万正整数的若干集合,作为构建不同规模查找表的基础; (2)基于散列表的工作原理,采用除留余数法H(key) = key % p (p<m) 作为散列函数,采用线性探测法处理冲突,实现散列查找算法,根据(1)中生成的正整数集合构造散列表,测量不同规模散列表的平均查找长度; (3)基于散列表的工作原理,采用除留余数法H(key) = key % p (p<m) 作为散列函数,采用链地址法处理冲突,实现散列查找算法,根据(1)中生成的正整数集合构造散列表,测量不同规模散列表的平均查找长度; (4)采用控制变量的原理设计实验并观测实验结果,探明散列表的长度m、散列函数的除数p、冲突处理方法、散列表的装填因子对散列函数平均查找长度的影响规律,形成结论。

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

// 定义散列表的最大长度
#define MAX_SIZE 100000

// 定义散列表节点
typedef struct Node {
    int key;
    struct Node* next;
} Node;

// 计算散列值
int hash(int key, int p) {
    return key % p;
}

// 插入节点到散列表
void insert(Node* hashTable[], int key, int p) {
    int index = hash(key, p);
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->next = NULL;

    if (hashTable[index] == NULL) {
        hashTable[index] = newNode;
    } else {
        Node* temp = hashTable[index];
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

// 线性探测法查找节点
int linearProbeSearch(Node* hashTable[], int key, int p) {
    int index = hash(key, p);
    int i = 0;
    while (hashTable[(index + i) % p] != NULL) {
        if (hashTable[(index + i) % p]->key == key) {
            return (index + i) % p;
        }
        i++;
    }
    return -1;
}

// 链地址法查找节点
int chainAddressingSearch(Node* hashTable[], int key, int p) {
    int index = hash(key, p);
    Node* temp = hashTable[index];
    while (temp != NULL) {
        if (temp->key == key) {
            return index;
        }
        temp = temp->next;
    }
    return -1;
}

// 计算平均查找长度
float calculateAverageSearchLength(Node* hashTable[], int size, int p, int (*search)(Node*[], int, int)) {
    int totalSearchLength = 0;
    int numSearches = 0;
    for (int i = 0; i < size; i++) {
        Node* temp = hashTable[i];
        while (temp != NULL) {
            int searchLength = search(hashTable, temp->key, p) + 1;
            totalSearchLength += searchLength;
            numSearches++;
            temp = temp->next;
        }
    }
    return (float)totalSearchLength / numSearches;
}

int main() {
    // 设置随机数种子
    srand(time(NULL));

    // 构建不同规模的正整数集合
    int size[] = {100, 1000, 10000};
    int numSizes = sizeof(size) / sizeof(size[0]);

    for (int i = 0; i < numSizes; i++) {
        int setSize = size[i];
        int integerSet[setSize];
        Node* hashTable[MAX_SIZE] = {NULL};

        printf("Generating set of size %d...\n", setSize);

        // 生成正整数集合
        for (int j = 0; j < setSize; j++) {
            integerSet[j] = rand() % (setSize * 10); // 此处设置正整数范围为0至setSize*10
        }

        printf("Constructing hash table using linear probing...\n");

        // 构建散列表并测量平均查找长度(线性探测法)
        float linearProbeASL = 0.0;
        int p = setSize / 2; // 除数p设置为集合大小的一半
        for (int j = 0; j < setSize; j++) {
            insert(hashTable, integerSet[j], p);
        }
        linearProbeASL = calculateAverageSearchLength(hashTable, MAX_SIZE, p, linearProbeSearch);
        
        printf("Average search length using linear probing: %.2f\n", linearProbeASL);

        printf("Constructing hash table using chaining...\n");

        // 构建散列表并测量平均查找长度(链地址法)
        float chainAddressingASL = 0.0;
        for (int j = 0; j < setSize; j++) {
            insert(hashTable, integerSet[j], p);
        }
        chainAddressingASL = calculateAverageSearchLength(hashTable, MAX_SIZE, p, chainAddressingSearch);
        
        printf("Average search length using chaining: %.2f\n", chainAddressingASL);

        printf("--------------------------------------------------\n");
    }
    
    return 0;
}

这段代码会根据题目要求,依次生成不同规模的正整数集合,并使用线性探测法和链地址法构建散列表,测量它们的平均查找长度。散列函数的除数p设置为集合大小的一半。最后,打印出每种冲突处理方法对应的平均查找长度。

注意:这段代码中使用了链表来处理冲突,但链表的长度并未限制。如果需要限制链表的最大长度,可以添加相应的逻辑进行处理。

问题(4)需要设计实验来观察散列表的长度m、散列函数的除数p、冲突处理方法、散列表的装填因子对散列函数平均查找长度的影响规律,并形成结论。下面是一个基本的实验设计:

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

#define MAX_SIZE 100000

typedef struct Node {
    int key;
    struct Node* next;
} Node;

int hash(int key, int p) {
    return key % p; // 除留余数法散列函数
}

void insert(Node* hashTable[], int key, int p) {
    int index = hash(key, p); // 计算散列值
    Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点
    newNode->key = key;
    newNode->next = NULL;

    if (hashTable[index] == NULL) {
        hashTable[index] = newNode; // 直接插入空槽位
    } else {
        Node* temp = hashTable[index];
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode; // 链接到链表末尾
    }
}

int linearProbeSearch(Node* hashTable[], int key, int p) {
    int index = hash(key, p); // 计算初始散列值
    int i = 0;
    while (hashTable[(index + i) % p] != NULL) { // 线性探测冲突处理
        if (hashTable[(index + i) % p]->key == key) {
            return (index + i) % p; // 找到目标键值,返回位置
        }
        i++;
    }
    return -1; // 没有找到目标键值
}

int chainAddressingSearch(Node* hashTable[], int key, int p) {
    int index = hash(key, p); // 计算散列值
    Node* temp = hashTable[index];
    while (temp != NULL) {
        if (temp->key == key) {
            return index; // 找到目标键值,返回位置
        }
        temp = temp->next;
    }
    return -1; // 没有找到目标键值
}

float calculateAverageSearchLength(Node* hashTable[], int size, int p, int (*search)(Node*[], int, int)) {
    int totalSearchLength = 0;
    int numSearches = 0;
    for (int i = 0; i < size; i++) {
        Node* temp = hashTable[i];
        while (temp != NULL) {
            int searchLength = search(hashTable, temp->key, p) + 1; // 查找目标键值并计算查找长度
            totalSearchLength += searchLength;
            numSearches++;
            temp = temp->next;
        }
    }
    return (float)totalSearchLength / numSearches; // 返回平均查找长度
}

void runExperiment(int setSize, int p, float loadFactor) {
    int numKeys = setSize * loadFactor; // 计算实际插入的键值对数量
    int integerSet[setSize];
    Node* hashTable[MAX_SIZE] = {NULL}; // 初始化散列表

    for (int j = 0; j < setSize; j++) {
        integerSet[j] = rand() % (setSize * 10); // 生成随机正整数集合
    }

    for (int j = 0; j < numKeys; j++) {
        insert(hashTable, integerSet[j], p); // 插入键值对到散列表
    }

    float linearProbeASL = calculateAverageSearchLength(hashTable, setSize, p, linearProbeSearch); // 计算线性探测法的平均查找长度
    float chainAddressingASL = calculateAverageSearchLength(hashTable, setSize, p, chainAddressingSearch); // 计算链地址法的平均查找长度

    printf("Experiment Results:\n");
    printf("Set Size: %d\n", setSize);
    printf("Number of Keys: %d\n", numKeys);
    printf("Load Factor: %.2f\n", loadFactor);
    printf("Hash Function Divisor (p): %d\n", p);
    printf("Linear Probing ASL: %.2f\n", linearProbeASL);
    printf("Chain Addressing ASL: %.2f\n", chainAddressingASL);
    printf("\n");
}

int main() {
    srand(time(NULL)); // 初始化随机数生成器

    // 实验参数
    int setSize = 10000; // 集合大小
    float loadFactors[] = {0.5, 0.7, 0.9}; // 装填因子
    int pValues[] = {100, 1000, 10000}; // 除数p的取值

    for (int i = 0; i < sizeof(loadFactors) / sizeof(loadFactors[0]); i++) {
        for (int j = 0; j < sizeof(pValues) / sizeof(pValues[0]); j++) {
            runExperiment(setSize, pValues[j], loadFactors[i]); // 执行实验
        }
    }

    return 0;
}

以上代码设计了一个实验来观察散列表的长度m、散列函数的除数p、冲突处理方法、散列表的装填因子对散列函数平均查找长度的影响规律。实验中,设置了固定的集合大小、不同的装填因子和不同的除数p的取值。通过运行实验,可以观察不同参数对平均查找长度的影响,进而得出结论。

请注意,实验设计可以根据实际需求进行修改和扩展。例如,你可以更改集合大小、装填因子和除数p的取值范围,或者添加更多的参数进行实验。同时,你还可以修改输出方式,以便更好地呈现实验结果和结论。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值