C++ Map(list与数组的结合char型)(hash算法.)
2016-04-10 19:16
344 查看
//注意char类型的map是需要用字符串判断所get的key是否与存储的key相等的,所以不再是map->key == key;这里要注意,还有,一开始我做的时候不明白为什么hash要*33,后来明白,其实是冲突防止会分布不均匀,因为hash需要的就是提高效率,所以*33拉开距离,使得存储尽可能的均匀,当然因为所得数字肯定是会很大的,所以还是一样用了hash的取余法再次进行取余分配到1024数组中。
.c文件
#ifndef __MAP_H__
#define __MAP_H__
#include "typedef.h"
typedef struct MapItem MapItem;
typedef struct Map Map;
struct MapItem{
MapItem* next;
void* value;
char* key;
};
struct Map{
MapItem* mArr[1024];
};
my_extern void MapInit(Map* map);
my_extern void MapSet(Map* map, char* key, void* value);
my_extern void* MapGet(Map* map, char* key);
my_extern int my_strcmp(const char* str1, const char* str2);
my_extern unsigned int Hash33(char *str);
my_extern char* strCopy(char* str);
#endif
.h文件
#include "Map.h"
#include "stdafx.h"
#include <stdlib.h>
#include "string.h"
void MapInit(Map* map){
int i = 1024;
while (i>=0 )
{
--i;
map->mArr[i] = NULL;
}
}
void MapSet(Map* map, char* key, void* value){
MapItem* p;
int strKey = Hash33(strCopy(key));
p = (MapItem*)malloc(sizeof(MapItem));
if (map->mArr[strKey % 1024] == NULL)
{
map->mArr[strKey % 1024] = p;
p->key = key;
p->value = value;
p->next = NULL;
}
else
{
p->next = map->mArr[strKey % 1024];
map->mArr[strKey % 1024] = p;
p->key = key;
p->value = value;
}
}
void* MapGet(Map* map, char* key){
MapItem* p;
int strKey = Hash33(strCopy(key));
p = map->mArr[strKey % 1024];
while (p != NULL)
{
if (my_strcmp(p->key,key)==0)
{
return p->value;
}
p = p->next;
}
return 0;
}
char* strCopy(char* str){
int keyNum = strlen(str) + 1;
char *keyHash = (char*)malloc(keyNum);
char *keyHash2 = keyHash;
while (*str != '\0')
{
*keyHash++ = *str++;
printf("%s", keyHash2);
}
*keyHash = '\0';
printf("%s", keyHash2);
return keyHash2;
}
unsigned int Hash33(char *str)
{
unsigned int hash = 0;
while (*str)
{
hash = hash*33 + (*str++);
}
return hash;
}
int my_strcmp(const char* str1, const char* str2){
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 == *str2)
{
*str1++;
*str2++;
continue;
}
else if (*str1 < *str2)
{
*str1++;
*str2++;
return -1;
}
else if (*str1 > *str2)
{
*str1++;
*str2++;
return 1;
}
}
return 0;
}
.c文件
#ifndef __MAP_H__
#define __MAP_H__
#include "typedef.h"
typedef struct MapItem MapItem;
typedef struct Map Map;
struct MapItem{
MapItem* next;
void* value;
char* key;
};
struct Map{
MapItem* mArr[1024];
};
my_extern void MapInit(Map* map);
my_extern void MapSet(Map* map, char* key, void* value);
my_extern void* MapGet(Map* map, char* key);
my_extern int my_strcmp(const char* str1, const char* str2);
my_extern unsigned int Hash33(char *str);
my_extern char* strCopy(char* str);
#endif
.h文件
#include "Map.h"
#include "stdafx.h"
#include <stdlib.h>
#include "string.h"
void MapInit(Map* map){
int i = 1024;
while (i>=0 )
{
--i;
map->mArr[i] = NULL;
}
}
void MapSet(Map* map, char* key, void* value){
MapItem* p;
int strKey = Hash33(strCopy(key));
p = (MapItem*)malloc(sizeof(MapItem));
if (map->mArr[strKey % 1024] == NULL)
{
map->mArr[strKey % 1024] = p;
p->key = key;
p->value = value;
p->next = NULL;
}
else
{
p->next = map->mArr[strKey % 1024];
map->mArr[strKey % 1024] = p;
p->key = key;
p->value = value;
}
}
void* MapGet(Map* map, char* key){
MapItem* p;
int strKey = Hash33(strCopy(key));
p = map->mArr[strKey % 1024];
while (p != NULL)
{
if (my_strcmp(p->key,key)==0)
{
return p->value;
}
p = p->next;
}
return 0;
}
char* strCopy(char* str){
int keyNum = strlen(str) + 1;
char *keyHash = (char*)malloc(keyNum);
char *keyHash2 = keyHash;
while (*str != '\0')
{
*keyHash++ = *str++;
printf("%s", keyHash2);
}
*keyHash = '\0';
printf("%s", keyHash2);
return keyHash2;
}
unsigned int Hash33(char *str)
{
unsigned int hash = 0;
while (*str)
{
hash = hash*33 + (*str++);
}
return hash;
}
int my_strcmp(const char* str1, const char* str2){
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 == *str2)
{
*str1++;
*str2++;
continue;
}
else if (*str1 < *str2)
{
*str1++;
*str2++;
return -1;
}
else if (*str1 > *str2)
{
*str1++;
*str2++;
return 1;
}
}
return 0;
}
相关文章推荐
- C++ Map(list与数组的结合int型)(hash算法.)
- C++ 模板特化
- C语言中,数组名作为参数传递给函数时,退化为指针
- 第10课 C++中的新成员
- C++封装、继承、多态
- 大话设计模式C++实现-第22章-桥接模式
- C++: string的大小写转换
- C++实验3-个人所得税计算
- C++第3次上机实验报告(友元函数二题)
- 1025_完数
- C语言浅整理
- c++作业3
- C语言基础
- 初学者刚学c++在定义类时只有成员变量易犯的错误
- C++实现——Remove Invalid Parentheses
- 桶式排序(C语言)
- C语言中整型与指针之间转换的注意点
- C++实验3
- 我的C++第三次上机作业
- C++中volatile关键字