0% found this document useful (0 votes)
158 views4 pages

Hash Tables

The document describes the implementation of open hashing and closed hashing using linked lists and arrays respectively. For open hashing, functions are provided to initialize a hash table, search for a key, and insert a new key. For closed hashing, a hash entry structure is defined containing the element and its status. Functions are similarly defined for initialization, quadratic probing search and insertion. Rehashing is also implemented to resize the table when loading increases.

Uploaded by

Albi Banushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views4 pages

Hash Tables

The document describes the implementation of open hashing and closed hashing using linked lists and arrays respectively. For open hashing, functions are provided to initialize a hash table, search for a key, and insert a new key. For closed hashing, a hash entry structure is defined containing the element and its status. Functions are similarly defined for initialization, quadratic probing search and insertion. Rehashing is also implemented to resize the table when loading increases.

Uploaded by

Albi Banushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/* TABELAT HASH */

/* Implementimi i hashit te hapur */


typedef struct list_node *node_ptr;
struct list_node
{
element_type element;
node_ptr next;
};
typedef node_ptr LIST;
typedef node_ptr position;
struct hash_tbl
{
int table_size;
LIST *the_lists;
};
typedef struct hash_tbl *HASH_TABLE;
HASH_TABLE initialize_table( int table_size )
{
HASH_TABLE H;
int i;
if( table size < MIN_TABLE_SIZE )
{
error("Table size too small");
return NULL;
}
H = (HASH_TABLE) malloc ( sizeof (struct hash_tbl) );
if( H == NULL )
fatal_error("Out of space!!!");
H->table_size = next_prime( table_size );
H->the_lists = (position *)
malloc( sizeof (LIST) * H->table_size );
if( H->the_lists == NULL )
fatal_error("Out of space!!!");
for(i=0; i<H->table_size; i++ )
{
H->the_lists[i] = (LIST) malloc( sizeof (struct list_node)
);
if( H->the_lists[i] == NULL )
fatal_error("Out of space!!!");
else
H->the_lists[i]->next = NULL;
}
return H;
}

// Implementimi i kerkimit ne hash te hapur


position gjej( element_type key, HASH_TABLE H )
{
position p;
LIST L;
L = H->the_lists[ hash( key, H->table_size) ];
p = L->next;
while( (p != NULL) && (p->element != key) )
/* mund te duhet strcmp!! */
p = p->next;
return p;
}
// Implementimi i shtimit ne hash te hapur
void insert( element_type key, HASH_TABLE H )
{
position pos, new_cell;
LIST L;
pos = gjej( key, H );
if( pos == NULL )
{
new_cell = (position) malloc(sizeof(struct list_node));
if( new_cell == NULL )
fatal_error("Out of space!!!");
else
{
L = H->the_lists[ hash( key, H->table size ) ];
new_cell->next = L->next;
new_cell->element = key;
L->next = new_cell;
}
}
}
/* Implementimi hash i mbyllur */
enum kind_of_entry { legitimate, empty, deleted };
struct hash_entry
{
element_type element;
enum kind_of_entry info;
};
typedef INDEX position;
typedef struct hash_entry cell;
struct hash_tbl
{
int table_size;
cell *the_cells;
};
typedef struct hash_tbl *HASH_TABLE;
HASH_TABLE initialize_table(int table_size )
{
HASH_TABLE H;
int i;
if( table_size < MIN_TABLE_SIZE )
{
error("Table size too small");

return NULL;
}
H = (HASH_TABLE) malloc( sizeof ( struct hash_tbl ) );
if( H == NULL )
fatal_error("Out of space!!!");
H->table_size = next_prime( table_size );
H->the cells = (cell *) malloc( sizeof ( cell ) * H->table_size );
if( H->the_cells == NULL )
fatal_error("Out of space!!!");
for(i=0; i<H->table_size; i++ )
H->the_cells[i].info = empty;
return H;
}
// Implementimi hash i mbyllur/Gjej kuadratik
position gjej( element_type key, HASH_TABLE H )
{
position i, current_pos;
i = 0;
current_pos = hash( key, H->table_size );
while( (H->the_cells[current_pos].element != key ) &&
(H->the_cells[current_pos].info != empty ) )
{
current_pos += 2*(++i) - 1;
if( current_pos >= H->table_size )
current_pos -= H->table_size;
}
return current_pos;
}
// Implementimi hash i mbyllur/Shto kuadratik
void shto( element_type key, HASH_TABLE H )
{
position pos;
pos = gjej( key, H );
if( H->the_cells[pos].info != legitimate )
{
H->the_cells[pos].info = legitimate;
H->the_cells[pos].element = key;
}
}
/* Implementimi rehash */
HASH_TABLE rehash( HASH_TABLE H )
{
int i, old_size;
cell *old_cells;
old_cells = H->the_cells;
old_size = H->table_size;
H = inicializo( 2*old_size );
for( i=0; i<old_size; i++ )
if( old_cells[i].info == legitimate )
insert( old_cells[i].element, H );
free( old_cells );
return H;
}

You might also like