69,382
社区成员
发帖
与我相关
我的任务
分享
/* HASH.H
**
** Header file for Hash Table module HASH.C
**
*/
#ifndef __HASH_H
#define __HASH_H
#define HTMAXSIZE 1999
#define NOTFOUND 0
#define MAXLEN 127
#ifdef __cplusplus
extern "C" {
#endif
struct HTdata //用户数据
{
char *key;
char *userName;
char *userPhone;
char *userCert;
};
typedef struct HTdata *HTelem;
struct HTentry //hash table 结构
{
HTelem elem;
struct HTentry *next;
};
typedef struct HTentry *HTtable;
HTtable *HTcreate(void); //创建hash table
int HTinsert(HTtable *, char*, HTelem ); //根据用户提供的关键字和数据插入表格中
HTelem HTfind(HTtable *, char * ); //根据关键字,查找数据
void HTfree(HTtable *); //释放hash table
#ifdef __cplusplus
}
#endif
#endif
/* HASH.C
**
** Implementation of a simple Hash Table for string storage & retrieval
**
** Written by L. Rossman
**
** The hash table data structure (HTable) is defined in "hash.h".
** Interface Functions:
** HTcreate() - creates a hash table
** HTinsert() - inserts a string & its index value into a hash table
** HTfind() - retrieves the index value of a string from a table
** HTfree() - frees a hash table
**
*/
#include <malloc.h>
#include <string.h>
#include "hash.h"
int hash(char *key) //hash索引值算法
{
int n = 0;
char *ch = key;
while (*ch)
{
n <<= 1;
n += (*ch);
++ch;
}
return(n % HTMAXSIZE);
}
HTtable *HTcreate() //创建hash table
{
int i;
HTtable *ht =
(HTtable *) calloc(HTMAXSIZE, sizeof(HTtable));
if (ht != NULL)
for (i=0; i<HTMAXSIZE; i++) ht[i] = NULL;
return(ht);
}
int HTinsert(HTtable *ht, char* key, HTelem elem) //数据插入hash table,成功返回1,失败返回0
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE) || (0==elem) ) return(0);
entry = (struct HTentry *) malloc(sizeof(struct HTentry));
if (entry == NULL) return(0);
elem->key = key;
entry->elem = elem;
entry->next = ht[i];
ht[i] = entry;
return(1);
}
HTelem HTfind(HTtable *ht, char* key) //根据关键字查找用户数据,找到返回用户数据指针,否则返回空
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE)) return(NOTFOUND);
entry = ht[i];
while (entry != NULL)
{
if (strcmp(entry->elem->key, key) == 0) return(entry->elem);
entry = entry->next;
}
return(NOTFOUND);
}
void HTfree(HTtable *ht) //清空hash table
{
struct HTentry *entry,
*nextentry;
int i;
for (i=0; i<HTMAXSIZE; i++)
{
entry = ht[i];
while (entry != NULL)
{
nextentry = entry->next;
free(entry);
entry = nextentry;
}
}
free(ht);
}
#include "stdio.h"
#include "hash.h"
int main()
{
struct HTdata data[] = {
{"1", "yyyapple", "777777", "1234567"},
{"2", "yyyapple2", "777777", "1234567"},
{"3", "yyyapple3", "777777", "1234567"}
};
HTtable* pt = HTcreate();
for(int i=0;i <3; i++)
{
HTinsert(pt, data[i].key, &data[i]);
}
HTelem p1 = HTfind(pt, "2");
printf("key = %s, userName = %s, userPhone = %s, userCert = %s\n",
p1->key, p1->userName, p1->userPhone, p1->userCert);
HTfree(pt);
return 0;
}
/* HASH.H
**
** Header file for Hash Table module HASH.C
**
*/
#define HTMAXSIZE 1999
#define NOTFOUND 0
struct HTentry
{
char *key;
int data;
struct HTentry *next;
};
typedef struct HTentry *HTtable;
HTtable *HTcreate(void);
int HTinsert(HTtable *, char *, int);
int HTfind(HTtable *, char *);
void HTfree(HTtable *);
/* HASH.C
**
** Implementation of a simple Hash Table for string storage & retrieval
**
** Written by L. Rossman
**
** The hash table data structure (HTable) is defined in "hash.h".
** Interface Functions:
** HTcreate() - creates a hash table
** HTinsert() - inserts a string & its index value into a hash table
** HTfind() - retrieves the index value of a string from a table
** HTfree() - frees a hash table
**
*/
#include <malloc.h>
#include <string.h>
#include "hash.h"
int hash(char *key)
{
int n = 0;
char *ch = key;
while (*ch)
{
n <<= 1;
n += (*ch);
++ch;
}
return(n % HTMAXSIZE);
}
HTtable *HTcreate()
{
int i;
HTtable *ht =
(HTtable *) calloc(HTMAXSIZE, sizeof(HTtable));
if (ht != NULL)
for (i=0; i<HTMAXSIZE; i++) ht[i] = NULL;
return(ht);
}
int HTinsert(HTtable *ht, char *key, int data)
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE)) return(0);
entry = (struct HTentry *) malloc(sizeof(struct HTentry));
if (entry == NULL) return(0);
entry->key = key;
entry->data = data;
entry->next = ht[i];
ht[i] = entry;
return(1);
}
int HTfind(HTtable *ht, char *key)
{
int i = hash(key);
struct HTentry *entry;
if ((i < 0) || (i >= HTMAXSIZE)) return(NOTFOUND);
entry = ht[i];
while (entry != NULL)
{
if (strcmp(entry->key,key) == 0) return(entry->data);
entry = entry->next;
}
return(NOTFOUND);
}
void HTfree(HTtable *ht)
{
struct HTentry *entry,
*nextentry;
int i;
for (i=0; i<HTMAXSIZE; i++)
{
entry = ht[i];
while (entry != NULL)
{
nextentry = entry->next;
free(entry);
entry = nextentry;
}
}
free(ht);
}
/*
* Copyright (c) 1990, 1991 Stanford University
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Stanford not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Stanford makes no representations about
* the suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* UHash - hash table.
*/
#ifndef unidraw_uhash_h
#define unidraw_uhash_h
#include <InterViews/defs.h>
class Iterator;
class UList;
class UHashElem {
public:
UHashElem(void* = nil);
virtual ~UHashElem();
void* GetKey();
void SetKey(void*);
private:
void* _key;
};
inline void* UHashElem::GetKey () { return _key; }
inline void UHashElem::SetKey (void* key) { _key = key; }
class UHashTable {
public:
UHashTable(int nslots);
virtual ~UHashTable();
virtual void Register(void* key, UHashElem* = nil);
virtual void Unregister(void* key);
void First(Iterator&);
void Next(Iterator&);
boolean Done(Iterator);
UHashElem* GetElem(Iterator);
UHashElem* Find(void* key);
protected:
virtual UHashElem* CreateElem();
virtual int Hash(void*);
protected:
int _nslots;
private:
UList* UElem(Iterator);
UHashElem* Elem(UList*);
void DeleteSlot(UList*);
private:
UList** _slot;
};
#endif
/*
* Copyright (c) 1990, 1991 Stanford University
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Stanford not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Stanford makes no representations about
* the suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* UHash implementation.
*/
#include <Unidraw/iterator.h>
#include <Unidraw/uhash.h>
#include <Unidraw/ulist.h>
#include <OS/memory.h>
/*****************************************************************************/
UHashElem::UHashElem (void* key) { _key = key; }
UHashElem::~UHashElem () { }
/*****************************************************************************/
UHashElem* UHashTable::CreateElem () { return nil; }
UHashElem* UHashTable::Elem (UList* r) { return (UHashElem*) (*r)(); }
int UHashTable::Hash (void* c) { return (unsigned) c % _nslots; }
UHashTable::UHashTable (int nslots) {
_nslots = nslots;
_slot = new UList*[_nslots];
Memory::zero(_slot, sizeof(void*) * _nslots);
}
UHashTable::~UHashTable () {
for (int i = 0; i < _nslots; ++i) {
if (_slot[i] != nil) {
DeleteSlot(_slot[i]);
}
}
delete _slot;
}
void UHashTable::Register (void* key, UHashElem* elem) {
int n = Hash(key);
if (_slot[n] == nil)
{
_slot[n] = new UList;
}
if (elem == nil) {
elem = CreateElem();
}
elem->SetKey(key);
_slot[n]->Prepend(new UList(elem));
}
void UHashTable::Unregister (void* key) {
int n = Hash(key);
UList* slot = _slot[n];
if (slot != nil) {
for (UList* u = slot->First(); u != slot->End(); u = u->Next()) {
UHashElem* elem = Elem(u);
if (elem->GetKey() == key) {
slot->Remove(u);
delete elem;
delete u;
break;
}
}
}
}
UList* UHashTable::UElem (Iterator i) { return (UList*) i.GetValue(); }
UHashElem* UHashTable::GetElem (Iterator i) { return Elem(UElem(i)); }
void UHashTable::First (Iterator& i) {
for (int j = 0; j < _nslots; ++j) {
if (_slot[j] != nil) {
i.SetValue(_slot[j]->First());
return;
}
}
i.SetValue(nil);
}
void UHashTable::Next (Iterator& i) {
UHashElem* elem = GetElem(i);
if (elem != nil) {
int n = Hash(elem->GetKey());
UList* u = UElem(i);
u = u->Next();
if (u == _slot[n]->End()) {
for (int j = n+1; j < _nslots; ++j) {
if (_slot[j] != nil) {
u = _slot[j]->First();
break;
}
}
}
i.SetValue(u);
}
}
boolean UHashTable::Done (Iterator i) {
for (int j = _nslots - 1; j >= 0; --j) {
if (_slot[j] != nil) {
return UElem(i) == _slot[j]->End();
}
}
return true;
}
UHashElem* UHashTable::Find (void* key) {
int n = Hash(key);
UList* slot = _slot[n];
if (slot != nil) {
for (UList* u = slot->First(); u != slot->End(); u = u->Next()) {
UHashElem* elem = Elem(u);
if (elem->GetKey() == key) {
return elem;
}
}
}
return nil;
}
void UHashTable::DeleteSlot (UList* slot) {
while (!slot->IsEmpty()) {
UList* u = slot->First();
slot->Remove(u);
UHashElem* elem = Elem(u);
delete elem;
delete u;
}
delete slot;
}