0% found this document useful (0 votes)
118 views3 pages

Tabele de Dispersie

This document defines functions to implement a hash table in C using linked lists to handle collisions. It includes functions to create and clear the table, insert and search values, and clear individual keys. The hash table uses a struct to define the table itself with a size and pointer to an array of linked list heads. Each list node stores a string and pointer to the next node. Functions are provided to hash string keys, search for keys, insert new keys into the linked list bucket, and clear individual keys or the entire table.

Uploaded by

Robert Pop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

Tabele de Dispersie

This document defines functions to implement a hash table in C using linked lists to handle collisions. It includes functions to create and clear the table, insert and search values, and clear individual keys. The hash table uses a struct to define the table itself with a size and pointer to an array of linked list heads. Each list node stores a string and pointer to the next node. Functions are provided to hash string keys, search for keys, insert new keys into the linked list bucket, and clear individual keys or the entire table.

Uploaded by

Robert Pop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<conio.h> #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.

h> //We need to define our list structure typedef struct _List_t{ char *string; struct _List_t *next; }List_t; //We need to define our hash table itself typedef struct _HashTable_t{ int size; List_t **table; }HashTable_t; HashTable_t *CreateTable(int); HashTable_t *CreateTable(int size) { HashTable_t *NewTable; //We wil need to alocate memory both for the table structure //and the table itself NewTable=(HashTable_t*)malloc(sizeof(HashTable_t)); NewTable->table=(List_t**)malloc(sizeof(HashTable_t)); //Initially we will need for the table elements to be NULL for(int i=0;i<size;i++) NewTable->table[i]=NULL; //We also need to set size of the table NewTable->size=size; return NewTable; } unsigned int hash(HashTable_t *hashtable, char *str) { unsigned int hashval=0; if (str!=NULL) hashval=hashval*37 + *str; *(str+1); return hashval%hashtable->size; } //We need to define our search function List_t *search(HashTable_t *hashtable, char *str) { List_t *list; unsigned int hashval=hash(hashtable,str); for(list=hashtable->table[hashval];list!=NULL;list=list->next) if(strcmp(str,list->string)==0) { return list; } return NULL; }

//We need the insertion function int insert(HashTable_t *hashtable, char *str) { List_t *NewList; List_t *CurentList; unsigned int hashval=hash(hashtable,str); NewList=(List_t*)malloc(sizeof(List_t)); //Is the item already in the list? CurentList=search(hashtable,str); //Handling a positive search if(CurentList !=NULL) return 2; //Finally the insertion NewList->string=_strdup(str); NewList->next=hashtable->table[hashval]; hashtable->table[hashval]=NewList; return 0; } //We'll need to implement 2 clear functions:one for clearing a specific value i n the table, and another one to clear up the table itself void ClearKey(HashTable_t *hashtable,char *str); void ClearKey(HashTable_t *hashtable,char *str){ List_t *list,*temp; int hashval=hash(hashtable,str); //We position ourselves on index of the table, because that's where our head of the list is list=hashtable->table[hashval]; while(list!=NULL){ //If we found what we are looking for erase it if(list->string=str) temp=list; list=list->next; free(temp); } } void ClearTable(HashTable_t); void ClearTable(HashTable_t *hashtable) { List_t *list,*temp; if(hashtable!=NULL) for(int i=0;i<hashtable->size;i++){ list=hashtable->table[i]; while(list!=NULL){ temp=list; list=list->next; free(temp->string); free(temp); } } free(hashtable->table); free(hashtable); } void main() {

char sel,s[12]; HashTable_t *MyHashTable; int TableSize=12; MyHashTable=CreateTable(TableSize); printf("Select an operation\n"); printf("1. Insert 2. Search 3. Erase Other=Quit\n"); sel=getch(); switch(sel) { case 'l': printf("Enter the key:\n"); scanf("%c",&s); insert(MyHashTable,s); break; case '2': printf("What key are you looking for?\n"); scanf("%c",&s); search(MyHashTable,s); break; case '3': printf("What key do you aim to delete?\n"); scanf("%c",&s); ClearKey(MyHashTable,s); break; case '4': ClearTable(MyHashTable); break; default: printf("Invalid option"); } }

You might also like