其他分享
首页 > 其他分享> > 数据结构NOJ——23构造哈希表

数据结构NOJ——23构造哈希表

作者:互联网

在这里插入图片描述

#include<iostream>
using namespace std;

#define SIZE 11
#define NULLKEY 0

typedef struct {
	int key;
	int flag;//计算地址的次数
}RecordType,*HashTable;


void HashSreach(HashTable ht,int data[]) {
	for (int i = 0; i <SIZE; i++) {
		ht[i].key = 0;
		ht[i].flag = 0;
	}

	int address,sum;
	for (int i = 0; i < 8; i++) {
		address = (3 * data[i]) % 11;
		sum = 1;
		if (ht[address].key == 0) {//该地址未放入关键字
			ht[address].key = data[i];
			ht[address].flag=1;
		}
		else {//发生冲突
			do{
				address = (address + 1) % 11;
				sum = sum + 1;
			} while (ht[address].key != 0);
			ht[address].key = data[i];
			ht[address].flag = sum;
		}

	}
}

void ASL(HashTable ht) {
	int sum = 0;
	for (int i = 0; i < SIZE ; i++) {
		if (ht[i].key)
			sum += ht[i].flag;
	}
	double  ASL = sum / 8;
	cout << ASL;
}


void main() {
	int data[8] = { 22,41,53,46,30,13,01,67 };
	HashTable ht = (RecordType*)malloc(SIZE * sizeof(RecordType));
	HashSreach(ht, data);
	ASL(ht);
}

标签:key,NOJ,23,int,sum,ht,哈希,address,data
来源: https://blog.csdn.net/m0_56046092/article/details/120243871