数据结构与算法分析-分离链接散列表的实现

 

#ifndef _HASH_SC_H_ #define _HASH_SC_H_ struct ListNode; typedef struct ListNode *Postion; struct HashTbl; typedef struct HashTbl *HashTable; HashTable InitializeTable( int TableSize ); void DestoryTable( HashTable H ); Postion Find( char *Key, HashTable H ); void Insert( char *Key, HashTable H ); void Delete( char *Key, HashTable H ); int Retrieve( Postion P ); #endif

 

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <assert.h> #include <string.h> #include "hashsc.h" struct ListNode { char *Element; Postion Next; }; typedef Postion List; struct HashTbl { int TableSize; List *TheLists; }; int Hash( const char *Key, int TableSize ) { unsigned int HashVal = 0; while( *Key != '/0' ) { HashVal = ( HashVal << 5 ) + *Key++; } HashVal = HashVal % TableSize; return HashVal; } unsigned long NextPrime( unsigned long n ) { return n/2+1; } int MinTableSize = 15; HashTable InitializeTable( int TableSize ) { HashTable H; int i; if ( TableSize < MinTableSize ) { printf("Table size too small/n"); return NULL; } H = malloc( sizeof( struct HashTbl ) ); if ( H == NULL ){ printf("no memory/n"); return NULL; } H->TableSize = NextPrime( TableSize ); H->TheLists = malloc( sizeof( List ) * H->TableSize ); if ( H->TheLists == NULL ){ printf("no memory/n"); return NULL; } for ( i = 0; i < H->TableSize; i++ ) { H->TheLists[i] = malloc( sizeof(struct ListNode ) ); if ( H->TheLists[i] == NULL ) { printf("no memory/n"); return NULL; } else H->TheLists[i]->Next = NULL; } return H; } Postion Find( char *Key, HashTable H ) { Postion P; List L; L = H->TheLists[ Hash( Key, H->TableSize ) ]; P = L->Next; while( P != NULL && strcmp( Key, P->Element ) != 0 ) P = P->Next; return P; } void Insert( char *Key, HashTable H ) { Postion Pos, NewCell; List L; Pos = Find( Key, H ); if ( Pos == NULL ) { NewCell = malloc( sizeof( struct ListNode ) ); if ( NewCell == NULL ) { printf("no Memory/n"); return; } else{ L = H->TheLists[ Hash( Key, H->TableSize ) ]; NewCell->Next = L->Next; NewCell->Element = malloc( strlen( Key ) + 1 ); if ( NewCell->Element == NULL ) { printf("no Memory/n"); return; } strcpy( NewCell->Element, Key ); NewCell->Element[strlen(Key)] = '/0'; L->Next = NewCell; } } } void Delete( char *Key, HashTable H ) { Postion Pos, Pre; List L; int i; Pos = Find( Key, H ); if ( Pos == NULL ) return; else { L = H->TheLists[ Hash( Key, H->TableSize ) ]; for ( Pre = L ; Pre->Next != NULL; Pre = Pre->Next ) { if ( Pre->Next == Pos ) { Pre->Next = Pos->Next; free( Pos->Element ); free( Pos ); break; } } } return; } int main( int argc, char **argv ) { HashTable table; int i; List L; Postion pos; table = InitializeTable( 17 ); Insert( "10", table ); Insert( "20", table ); Insert( "30", table ); Insert( "40", table ); Insert( "50", table ); Insert( "60", table ); Insert( "70", table ); Insert( "80", table ); Insert( "90", table ); Insert( "91", table ); Insert( "92", table ); Insert( "93", table ); Insert( "97", table ); Insert( "95", table ); Insert( "96", table ); Insert( "99", table ); for ( i = 0; i < table->TableSize; i++ ) { printf("---hash %d---/n", i); L = table->TheLists[i]; pos = L->Next; while ( pos != NULL ) { printf("--%s--/n", pos->Element); pos = pos->Next; } } printf("/n/n/n/n/n"); Delete( "60", table ); Delete( "70", table ); for ( i = 0; i < table->TableSize; i++ ) { printf("---hash %d---/n", i); L = table->TheLists[i]; pos = L->Next; while ( pos != NULL ) { printf("--%s--/n", pos->Element); pos = pos->Next; } } return 0; }

你可能感兴趣的:(数据结构与算法分析-分离链接散列表的实现)