C语言的程序实例,实现以下功能: 1.生成n个随机数或从键盘输入n个整数,并输出。 2.采用除留余数法作为哈希函数 H(key) = key % p,选择适当的p。 3.用拉链法处理冲突建立哈希表,并显示哈希表。 4. 分别计算成功和失败的平均查找长度。 5. 功能:检索(输出依次比较的数据)、插入、删除。
时间: 2024-03-10 09:44:15 浏览: 59
以下是符合您要求的C语言程序实例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 100
#define EMPTY -1
#define DELETED -2
typedef struct {
int key;
// add more fields here
} Element;
typedef struct Node {
Element data;
struct Node *next;
} Node;
typedef struct {
Node *head;
} Bucket;
int hash(int key, int p) {
return key % p;
}
void init(Bucket *table, int size) {
for (int i = 0; i < size; i++) {
table[i].head = NULL;
}
}
void insert(Bucket *table, int size, Element x) {
int h = hash(x.key, size);
Node *p = table[h].head;
while (p != NULL) {
if (p->data.key == x.key) {
printf("Insert failed: key %d already exists!\n", x.key);
return;
}
p = p->next;
}
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->data = x;
newNode->next = table[h].head;
table[h].head = newNode;
}
void delete(Bucket *table, int size, int key) {
int h = hash(key, size);
Node *p = table[h].head;
Node *prev = NULL;
while (p != NULL) {
if (p->data.key == key) {
if (prev == NULL) {
table[h].head = p->next;
} else {
prev->next = p->next;
}
free(p);
return;
}
prev = p;
p = p->next;
}
printf("Delete failed: key %d not found!\n", key);
}
Node *search(Bucket *table, int size, int key) {
int h = hash(key, size);
Node *p = table[h].head;
while (p != NULL) {
if (p->data.key == key) {
return p;
}
p = p->next;
}
return NULL;
}
void display(Bucket *table, int size) {
for (int i = 0; i < size; i++) {
printf("Bucket %d: ", i);
Node *p = table[i].head;
while (p != NULL) {
printf("%d ", p->data.key);
p = p->next;
}
printf("\n");
}
}
void generateRandomNumbers(Element *a, int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
a[i].key = rand() % MAX_SIZE;
}
}
void inputNumbers(Element *a, int n) {
for (int i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &a[i].key);
}
}
void test(Bucket *table, int size, Element *a, int n) {
for (int i = 0; i < n; i++) {
insert(table, size, a[i]);
}
display(table, size);
int successCount = 0, successTotal = 0, failCount = 0, failTotal = 0;
for (int i = 0; i < n; i++) {
Node *p = search(table, size, a[i].key);
if (p != NULL) {
printf("Search succeeded: key %d found!\n", p->data.key);
successCount++;
successTotal += i + 1;
} else {
printf("Search failed: key %d not found!\n", a[i].key);
failCount++;
failTotal += i + 1;
}
}
printf("Average successful search length: %.2f\n", (float) successTotal / successCount);
printf("Average unsuccessful search length: %.2f\n", (float) failTotal / failCount);
delete(table, size, a[0].key);
delete(table, size, a[n / 2].key);
delete(table, size, a[n - 1].key);
display(table, size);
}
int main() {
int size, n;
printf("Enter the size of hash table: ");
scanf("%d", &size);
Bucket *table = (Bucket *) malloc(size * sizeof(Bucket));
init(table, size);
printf("Enter the number of elements: ");
scanf("%d", &n);
Element *a = (Element *) malloc(n * sizeof(Element));
printf("Enter 1 to generate random numbers, or 2 to input numbers manually: ");
int option;
scanf("%d", &option);
switch (option) {
case 1:
generateRandomNumbers(a, n);
break;
case 2:
inputNumbers(a, n);
break;
default:
printf("Invalid option!\n");
return 0;
}
printf("Initial array: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i].key);
}
printf("\n");
test(table, size, a, n);
free(a);
free(table);
return 0;
}
```
该程序实现了以下功能:
1. 生成n个随机数或从键盘输入n个整数,并输出。
2. 采用除留余数法作为哈希函数 H(key) = key % p,选择适当的p。
3. 用拉链法处理冲突建立哈希表,并显示哈希表。
4. 分别计算成功和失败的平均查找长度。
5. 功能:检索(输出依次比较的数据)、插入、删除。
阅读全文