parent
df3da815d4
commit
005c8fc991
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include"List.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
int InitList(LinkList *L)
|
||||
{
|
||||
*L = (LinkList)malloc(sizeof(Node_List)); /* 产生头结点,并使L指向此头结点 */
|
||||
if (!(*L)) /* 存储分配失败 */
|
||||
return 0;
|
||||
(*L)->next = NULL; /* 指针域为空 */
|
||||
|
||||
return 1;
|
||||
}
|
||||
int ListInsert(LinkList *L, int i, int e)
|
||||
{
|
||||
int j;
|
||||
LinkList p, s;
|
||||
p = *L;
|
||||
j = 1;
|
||||
while (p && j < i) /* 寻找第i个结点 */
|
||||
{
|
||||
p = p->next;
|
||||
++j;
|
||||
}
|
||||
if (!p || j > i)
|
||||
return 0; /* 第i个元素不存在 */
|
||||
s = (LinkList)malloc(sizeof(Node_List)); /* 生成新结点(C语言标准函数) */
|
||||
s->data = e;
|
||||
s->next = p->next; /* 将p的后继结点赋值给s的后继 */
|
||||
p->next = s; /* 将s赋值给p的后继 */
|
||||
return 1;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
typedef struct Node_List
|
||||
{
|
||||
int data;
|
||||
struct Node_List *next;
|
||||
}Node_List;
|
||||
typedef struct Node_List *LinkList; //¶¨ÒåLinkList
|
||||
int InitList(LinkList *L);
|
||||
int ListInsert(LinkList *L, int i, int e);
|
@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
#include "hashTable.h"
|
||||
#define MAX_PRIME_LESS_THAN_HASH_LEN 7993
|
||||
char* strDup(const char* str)
|
||||
{
|
||||
int len;
|
||||
char* ret;
|
||||
if (str == NULL)return NULL;
|
||||
|
||||
len = strlen(str);
|
||||
ret = (char*)malloc(len + 1);
|
||||
if (ret != NULL) {
|
||||
memcpy(ret, str, len);
|
||||
ret[len] = '\0';
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
unsigned int hashfunc(char *str, int len)
|
||||
{
|
||||
register unsigned int sum = 0;
|
||||
register unsigned int h = 0;
|
||||
register char *p = str;
|
||||
while (p - str < len)
|
||||
{
|
||||
register unsigned short a = *(p++);
|
||||
sum ^= a * (p - str);
|
||||
h ^= a / (p - str);
|
||||
}
|
||||
return ((sum << 16) | h) % MAX_PRIME_LESS_THAN_HASH_LEN;
|
||||
}
|
||||
|
||||
|
||||
void initHashTable(hashTable* t)
|
||||
{
|
||||
int i;
|
||||
if (t == NULL)return;
|
||||
for (i = 0; i < BUCKETCOUNT; ++i) {
|
||||
t->bucket[i].key = NULL;
|
||||
t->bucket[i].value = NULL;
|
||||
t->bucket[i].next = NULL;
|
||||
}
|
||||
}
|
||||
void freeHashTable(hashTable* t)
|
||||
{
|
||||
int i;
|
||||
entry* e, *ep;
|
||||
if (t == NULL)return;
|
||||
for (i = 0; i<BUCKETCOUNT; ++i) {
|
||||
e = &(t->bucket[i]);
|
||||
while (e->next != NULL) {
|
||||
ep = e->next;
|
||||
e->next = ep->next;
|
||||
free(ep->key);
|
||||
free(ep->value);
|
||||
free(ep);
|
||||
}
|
||||
}
|
||||
}
|
||||
int insertEntry(hashTable* t, char *key, RBRoot *value)
|
||||
{
|
||||
int index, vlen1, vlen2;
|
||||
entry* e, *ep;
|
||||
|
||||
if (t == NULL || key == NULL || value == NULL) {
|
||||
return -1;
|
||||
}
|
||||
index = hashfunc(key, strlen(key));
|
||||
|
||||
if (t->bucket[index].key == NULL) {
|
||||
t->bucket[index].key = (char *)malloc(strlen(key)*sizeof(char));
|
||||
t->bucket[index].key = key;
|
||||
t->bucket[index].value = (RBRoot *)malloc(sizeof(RBRoot));
|
||||
t->bucket[index].value = value;
|
||||
}
|
||||
else {
|
||||
e = ep = &t->bucket[index];
|
||||
while (e != NULL) {
|
||||
if (strcmp(e->key, key) == 0)
|
||||
{
|
||||
memcpy(e->value, value, sizeof(RBRoot));
|
||||
return index;
|
||||
}
|
||||
ep = e;
|
||||
e = e->next;
|
||||
}
|
||||
e = (entry*)malloc(sizeof(entry));
|
||||
e->key = strDup(key);
|
||||
e->value= (RBRoot *)malloc(sizeof(RBRoot));
|
||||
e->value = value;
|
||||
e->next = NULL;
|
||||
ep->next = e;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
RBRoot* findValue(hashTable*t, char *key)
|
||||
{
|
||||
int index;
|
||||
entry* e;
|
||||
if (t == NULL || key == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
index = hashfunc(key,strlen(key));
|
||||
e = &(t->bucket[index]);
|
||||
if (e->key == NULL)return NULL;
|
||||
while (e != NULL)
|
||||
{
|
||||
if (strcmp(key, e->key)==0)
|
||||
{
|
||||
return e->value;
|
||||
}
|
||||
e = e->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "rbtree.h"
|
||||
#define BUCKETCOUNT 9000
|
||||
struct _hash_Entry
|
||||
{
|
||||
const char* key;//键
|
||||
RBRoot* value;//值
|
||||
struct _hash_Entry* next;//下一个结点
|
||||
};//结点的内容
|
||||
typedef struct _hash_Entry entry;
|
||||
typedef struct _Table
|
||||
{
|
||||
entry bucket[BUCKETCOUNT]; //定义桶
|
||||
}hashTable;
|
||||
char* strDup(const char* str);
|
||||
unsigned int hashfunc(char*key, int len);//哈希值的计算
|
||||
void initHashTable(hashTable* t);//初始化哈希表
|
||||
void freeHashTable(hashTable* t);//释放哈希表
|
||||
int insertEntry(hashTable* t, char *key, RBRoot *value);//插入数据
|
||||
RBRoot* findValue(hashTable*t, char *key);//查找
|
||||
//entry* removeTable(hashTable*t, char key);//删除,暂时不用
|
Loading…
Reference in new issue