MOOC清华《程序设计基础》第8章:哈希算法与哈希链表

一种将字符串映射成整数的简单哈希算法:

//累计异或得到哈希值——缺点:不同字符串有可能得到相同的哈希值 
int Hash(const char* str) 
{
	int sum = 0;
	for(int i = 0; i < strlen(str); i++)
		sum ^= str[i];
	return sum;
}
//  网上检查文件真伪的MD5码也是一种哈希值吗?雷同概率有多大?
//(人类的符号是有限的,所以组合结果也是有限的,必然有雷同)

对于文件“log.txt”的哈希算法的详细代码:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct Time_t      //定义结构体 
{
	int year, month, day;
	int hour, minute, second;
};

struct Node      //定义链表结点(其实链表就是一种特殊的结构体) 
{
	Time_t tm;
	char id[20];
	char op[10];     //以上三行为数据域 
	Node* next;     //这一行为指针域 
};

int Hash(const char* str) 
{
	int sum = 0;
	for(int i = 0; i < strlen(str); i++)
		sum ^= str[i];
	return sum;
}

void Insert(Node* hash_tab[], Node elem)
{
	//计算用户编号的哈希值
	int idx = Hash(elem.id);
	
	//添加新节点到链表头(成为新的头结点) 
	Node* data = new Node;
	*data = elem;
	
	data->next = hash_tab[idx];
	hash_tab[idx] = data; 
}

void Print(Node* list)
{
	
	//int cnt = 0;  
	while(list)  
	{  
    	//cout << cnt << ' ' << head->id << endl;  
    	cout << list->tm.year << '/' << list->tm.month << '/' << list->tm.day << ' ';
		cout << list->tm.hour << '/' << list->tm.minute << '/' << list->tm.second << ' ';  
		cout << list->id << ' ';
		cout << list->op << endl;
		//cnt++;  
    	//head = head->next;
		list = list->next;  
	} 
	
	/*
	//修改为输出到文件
	ofstream fout("hash.txt");
	while(list)  
	{  
    	//cout << cnt << ' ' << head->id << endl;  
    	fout << list->tm.year << '/' << list->tm.month << '/' << list->tm.day << ' ';
		fout << list->tm.hour << '/' << list->tm.minute << '/' << list->tm.second << ' ';  
		fout << list->id << ' ';
		fout << list->op << endl;
		//cnt++;  
    	//head = head->next;
		list = list->next;  
	} 	
	fout.close(); 
	*/	 
}

void Output(Node* hash_tab[])
{
	
	for(int i = 0; i < 256; i++)
	{
		if(hash_tab[i] == NULL) 
			continue;
		cout << i << ": ";
		Print(hash_tab[i]);
		cout << endl << endl;
	}
	
	/*
	//修改为输出到文件
	//ofstream fout("hash.txt");
	for(int i = 0; i < 256; i++)
	{
		if(hash_tab[i] == NULL) 
			continue;
		fout << i << ": ";
		Print(hash_tab[i]);
		fout << endl << endl;
	}
	//fout.close(); 
	*/
}

void Delete(Node* list)
{
	while(list)
	{
		Node* tmp = list;
		list = list->next;
		delete tmp;
	}
}

void Release(Node* hash_tab[])
{
	for(int i = 0; i < 256; i++)
	{
		if(hash_tab[i] == NULL)
			continue;
		Delete(hash_tab[i]);
		hash_tab[i] = NULL;
	}
}

int main()
{
	Node* list_tab[256] = {NULL};
	ifstream fin("log.txt");
	while(!fin.eof())
	{
		char tmp;
		Node data;
		
		//读入一行数据
		fin >> data.tm.year >> tmp >> data.tm.month >> tmp >> data.tm.day;
		fin >> data.tm.hour >> tmp >> data.tm.minute >> tmp >> data.tm.second;
		fin >> data.id;
		fin >> data.op;
		
		//添加至哈希链表
		Insert(list_tab, data); 
	}
	fin.close();
	
	//输出哈希表内容
	Output(list_tab);
	//修改“输出哈希表内容”为“输出哈希表内容到文件”,本条语句不变,修改函数内容 
	
	//释放哈希表内存
	Release(list_tab);
	
	return 0; 
}

 
《MOOC清华《程序设计基础》第8章:哈希算法与哈希链表》

《MOOC清华《程序设计基础》第8章:哈希算法与哈希链表》

最终的最大哈希值为111。我企图更改代码为输出到文件,可是没有修改成功。我估计需要把一些函数拆开,放在一个函数里面,才能实现输出到文件的操作。否则函数与函数之间的文件操作互相干涉,会出错。

    原文作者:哈希算法
    原文地址: https://blog.csdn.net/sunshineman1986/article/details/76559416
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞