C++-----自己写的哈希表
来源:互联网 发布:阿里云装mysql数据库 编辑:程序博客网 时间:2024/04/30 05:09
Node.CPP
#pragma once
template<class T,class U>class Node
{
public:
public:
int key;
int key1;
T data;
int count;
Node<T,U> *panother;
};
Nodeh
#include "StdAfx.h"
#include "Node.h"
MyHash.h
#pragma once
#include "Node.h"
#include<string>
using namespace std;
template<class T,class U>
class MyHash
{
public:
Node<T,U>*p;
T nullvalue;
public:
void put(U key,T data);
T find(U key);
private:
int creat_hash(int num);
int creat_key(int num);
int creat_hash(string num);
int creat_key(string num);
int change_int(int n);
int change_int(string da);
public:
MyHash(void);
~MyHash(void);
};
MyHash.CPP
#include "StdAfx.h"
#include "MyHash.h"
template<class T,class U>
MyHash<T,U>::MyHash(void)
{
p=new Node<T,U>[100];
}
template<class T,class U>
MyHash<T,U>::~MyHash(void)
{
delete []p;
}
template<class T,class U>
int MyHash<T,U>::creat_hash(int num){
return num%100;
}
template<class T,class U>
int MyHash<T,U>::creat_key(int num){
return num/100;
}
template<class T,class U>
int MyHash<T,U>::change_int(int n){
return n;
}
template<class T,class U>
int MyHash<T,U>::change_int(string da){
int temp=(int)(da[0]);
return temp;
}
template<class T,class U>
int MyHash<T,U>::creat_hash(string num){
int temp=(int)(num[0]);
//cout<<temp<<endl;
return temp%100;
}
template<class T,class U>
int MyHash<T,U>::creat_key(string num){
int temp=(int)(num[1]);
return temp/100;
}
template<class T,class U>
void MyHash<T,U>::put(U key,T data){
int id=this->creat_hash(key);
if(p[id].count!=1&&p[id].count!=2)
{
p[id].data=data;
p[id].count=1;
p[id].key=change_int(key);
p[id].key1=this->creat_key(key);
//cout<<p[id].key1<<endl;
}
else if(p[id].count==1){
p[id].panother=new Node<T,U>[100];
p[id].count=2;
int key1=this->creat_key(key);
int key2=this->creat_key(p[id].key);
p[id].panother[key1].data=data;
p[id].panother[key1].count=1;
p[id].panother[key1].key=key1;
p[id].panother[key2].data=p[id].data;
p[id].panother[key2].count=1;
p[id].panother[key2].key=key2;
}
else{
int key1=this->creat_key(key);
p[id].panother[key1].data=data;
p[id].panother[key1].count=1;
p[id].panother[key1].key=key1;
}
}
template<class T,class U>
T MyHash<T,U>::find(U key){
int id=this->creat_hash(key);
if(p[id].count==1)
{
if(p[id].key1==this->creat_key(key))
{
return p[id].data;
}
}
else if(p[id].count==2){
int key1=this->creat_key(key);
return p[id].panother[key1].data;
}
return this->nullvalue;
}
main
#include "stdafx.h"
#include "MyHash.cpp"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
MyHash<int,int> ihash;
ihash.put(3,56);
ihash.put(5,560);
/*string a="ddd";
ihash.put(a,90);
ihash.put("db",50);
ihash.put("dc",58);*/
//ihash.put("d",20);
//cout<<ihash.find("a")<<endl;
//char r=a[0];
cout<<ihash.find(5)<<endl;
return 0;
}
- C++-----自己写的哈希表
- 自己用C写的单链表
- 自己用C写的通讯录
- [C#]写自己的类库
- [c#][测试]自己写的混淆代码
- 自己写的C语言简单万年历
- 几个自己写的字符串C函数
- 评测自己写的Cherry-C编译器
- 自己写的c语言哥德巴赫猜想
- 自己c写的信息记录系统
- C调用自己写的动态库
- 自己写的递归快速排序(c++)
- 自己写的网上的一些试题C的实现
- 我的第一个完全自己写的C程序
- 自己写的基于SDL的C语言游戏!
- C语言的strcpy函数和自己写的函数
- C#: 自己写的一个类似BackGroundWorker的类
- 自己写的一个数据库操作类(C#)[原创]
- eclipse 开发环境下,提示 String cannot be resolved to a type
- MySQL之——主从server-id不生效,The server is not configured as slave
- 小猫统计——根据Excel文件或者文本文件自动建表
- Java、Spring 国际化
- android开发xml style使用
- C++-----自己写的哈希表
- 《剑指offer》不用加减乘除做加法
- c++实现八大排序方法(一)
- MongoDB学习之在Windows下安装MongoDB
- ./scripts/feeds update -a执行失败
- 字符串最后一个单词长度
- 四层和七层负载均衡
- LeetCode Largest Number
- Linux初学第二步——Ubuntu