//hash.c@standard c library(*SCL*) by ASHISH TIWARI
/*
You may print, store code from this program for your personal,
noncommercial use. By accessing this code, you agree not to reproduce,
distribute,
display or transmit information from this code in any form or by any means
without
the permission of ASHISH TIWARI, with this exception: You may occasionally
reproduce,
distribute, display or transmit an insubstantial portion of the information, for
a
noncommercial purpose, to a limited number of individuals. ASHISH TIWARI does
not warrant
the accuracy, completeness, timeliness, merchantability or fitness for a
particular purpose
of the cose,though every care has been taken in making this code and has been
compiled
on standard gcc compiler provided by GNU. ASHISH TIWARI shall not be liable for
any
errors or omissions in the information or decisions made or actions taken in
reliance
on the code.
"*SCL*" is a registered trademark of ASHISH TIWARI, IITKGP.
All contents of "*SCL*" is copyright ©2000-.... ,ASHISH TIWARI, IITKGP
unless otherwise noted. Reproduction in part or in whole without
permission is expressly prohibited.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 11
#define length 8
struct tag{
int data;
struct tag* link;
};
typedef struct tag node;
node* hashtable;
node* def_node(node* s,int i);
void insert(int hash,node* new);
void append(node* s,int data);
int hash_int(int input);
void print(node* s);
int hash_char(char*s,int* input);
int retrieve(int input);
void delete(node* s,int num);
main()
{
char* s;
node* new;
int i;
int input=100;int hash;
s=(char*)malloc(length*sizeof(char));
hashtable=(node*)malloc(maxsize*sizeof(node));
for(i=0;i<maxsize;i++)
{
hashtable[i].link=NULL;
hashtable[i].data=i;
}
while(input!=10 )
{
printf("chahiyekya\n");
scanf("%d",&input);
hash=hash_int(input);
new=def_node(new,input);
insert(hash,new);
}
for(i=0;i<maxsize;i++)
print(hashtable+i),printf("\n");
printf("\n\n\n\n");
i=retrieve(100);
for(i=0;i<maxsize;i++)
print(hashtable+i),printf("\n");
}
node* def_node(node* s,int i)
{
s=(node*)malloc(sizeof(node));
s->data=i;
s->link=NULL;
return(s);
}
void insert(int hash,node* new)
{
if(hashtable[hash].link==NULL)
hashtable[hash].link=new;
else
append(hashtable+hash,new->data);
}
void append(node* s,int data)
{
node* temp;
node* new;
temp=s;
if(temp==NULL)
{
temp=def_node(temp,data);
return;
}
while(temp->link!=NULL)
temp=temp->link;
new=def_node(new,data);
temp->link=new;
}
int hash_int(int input)
{
int i;
i=input%maxsize;
return(i);
}
void print(node* s)
{
node* temp;
temp=s;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->link;
}
printf("\n");
}
int hash_char(char*s,int* input)
{
int h=0;
while(*s)
h+=*s++;
*input=h;
return(abs(h%maxsize));
}
int retrieve(int input)
{
int i;
i=hash_int(input);
delete(hashtable[i].link,input);
return(input);
}
void delete(node* s,int num)
{
node* temp;
node* pre;
temp=s;
while(temp!=NULL)
{
if(s->data==num)
{
s=temp->link;
free(temp);
return;
}
else if(temp->data==num)
{
pre->link=temp->link;
free(temp);
return;
}
pre=temp;
temp=temp->link;
}
printf("element to b deleted not found\n");
}