Add to Favorites    Make Home Page 1430 Online  
 Language Categories  
 Our Services  

Home » C Home » Data Structures Home » A hashtable in C

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title A hashtable in C
Author Sourav Datta
Author Email soura_jagat [at] yahoo.com
Description A hashtable that stores name, value pairs as strings

Category C » Data Structures
Hits 397077
Code Select and Copy the Code
#include <string.h> #include <stdio.h> #include <stdlib.h> typedef struct _node{ char *name; char *desc; struct _node *next; }node; #define HASHSIZE 101 static node* hashtab[HASHSIZE]; void inithashtab(){ int i; for(i=0;i<HASHSIZE;i++) hashtab[i]=NULL; } unsigned int hash(char *s){ unsigned int h=0; for(;*s;s++) h=*s+h*31; return h%HASHSIZE; } node* lookup(char *n){ unsigned int hi=hash(n); node* np=hashtab[hi]; for(;np!=NULL;np=np->next){ if(!strcmp(np->name,n)) return np; } return NULL; } char* m_strdup(char *o){ int l=strlen(o)+1; char *ns=(char*)malloc(l*sizeof(char)); strcpy(ns,o); if(ns==NULL) return NULL; else return ns; } char* get(char* name){ node* n=lookup(name); if(n==NULL) return NULL; else return n->desc; } int install(char* name,char* desc){ unsigned int hi; node* np; if((np=lookup(name))==NULL){ hi=hash(name); np=(node*)malloc(sizeof(node)); if(np==NULL) return 0; np->name=m_strdup(name); if(np->name==NULL) return 0; np->next=hashtab[hi]; hashtab[hi]=np; } else free(np->desc); np->desc=m_strdup(desc); if(np->desc==NULL) return 0; return 1; } /* A pretty useless but good debugging function, which simply displays the hashtable in (key.value) pairs */ void displaytable(){ int i; node *t; for(i=0;i<HASHSIZE;i++){ if(hashtab[i]==NULL) printf("() "); else{ t=hashtab[i]; printf("("); for(;t!=NULL;t=t->next) printf("(%s.%s) ",t->name,t->desc); printf(") "); } } } void cleanup(){ int i; node *np,*t; for(i=0;i<HASHSIZE;i++){ if(hashtab[i]!=NULL){ np=hashtab[i]; while(np!=NULL){ t=np->next; free(np->name); free(np->desc); free(np); np=t; } } } } main(){ int i; char* names[]={"name","address","phone","k101","k110"}; char* descs[]={"Sourav","Sinagor","26300788","Value1","Value2"}; inithashtab(); for(i=0;i<5;i++) install(names[i],descs[i]); printf("Done "); printf("If we didnt do anything wrong.." "we should see %s ",get("k110")); install("phone","9433120451"); printf("Again if we go right, we have %s and %s ",get("k101"),get("phone")); /*displaytable();*/ cleanup(); return 0; }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi �դ ?�� ��??���
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1190


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review