哈希表中的一些问题

来源:互联网 发布:excel数据保护 编辑:程序博客网 时间:2024/05/17 06:01

‘`#include

include

include

include

include

include

include

using namespace std;//关于hw4中命令怎么写得问题
struct ListNode;
typedef unsigned int uint;
int size1 = 0;
uint string_to_int(string s)
{
uint k;
k= atoi(s.c_str());
return k;
}
struct elemtype
{
string str;
int type;
int fre;
};
struct ListNode
{
elemtype element;
struct ListNode *Next;
};
class HashTable
{
public:
int Tablesize;
ListNode **Thelists;
public:
HashTable Init(int Tablesize);
HashTable insert(elemtype Key, HashTable H1);
int Hash(elemtype Key, int Tablesize);
ListNode *Find(elemtype Key, HashTable H1);
HashTable Delete(elemtype Key, HashTable H1);
HashTable Rebuild(HashTable H1, int size, elemtype Harry[], int le, int fac);
HashTable Build(HashTable H1, int size, elemtype Harry[], int le, int fac);
};
HashTable HashTable::Init(int Tablesize)
{
HashTable H1;
int i;
H1.Tablesize = Tablesize;
// H1.Thelists = (ListNode**)malloc(sizeof(ListNode) * H1.Tablesize);
H1.Thelists = new ListNode*[H1.Tablesize];
if (H1.Thelists == NULL)
{
exit(0);
}
for (i = 0; i < H1.Tablesize; i++)
{
//H1.Thelists[i] = (ListNode )malloc(sizeof(struct ListNode));//从这一行开始读取字符串字符时出错,为啥好好的ListNode结构到这里成了ListNode
//但是同在结构体里的int类型能够读取//是不是malloc本身的问题
H1.Thelists[i] = new ListNode;
if (H1.Thelists[i] == NULL)
{
exit(0);
}
else
{
H1.Thelists[i]->Next = NULL;//位置冲突怎么避免
}

}return H1;

}
int HashTable::Hash(elemtype Key, int Tablesize)
{
uint hashval = 0;
uint hash_value = 0;
if (Key.type == 1)
{
hashval = string_to_int(Key.str);
hash_value = hashval % Tablesize;
}
if (Key.type == 0)
{
int lens = Key.str.length();
for (int i = 0; i

0 0
原创粉丝点击