Sie sind auf Seite 1von 6

#include<stdio.

h>
#include<stdlib.h>

struct listnode;

struct hashtbl;

struct listnode
{
int ele;
struct listnode *next;
};
typedef struct listnode *Position;

typedef Position list;


struct hashtbl
{
int tablesize;
list *thelist;
};
typedef struct hashtbl *ht;

ht initialise(int);
Position find(int,ht,int);
void insert(int,ht,int);
void insertline(int,ht,int);

void display();
ht initialise(int tablesize)
{
ht h;
int i;
h=malloc(sizeof(struct hashtbl));
h->tablesize=nextprime(tablesize);
h->thelist=malloc(sizeof(list)*h->tablesize);
for(i=0;i<h->tablesize-1;i++)
{
h->thelist[i]=malloc(sizeof(struct listnode));
if(h->thelist[i]==NULL)
printf("\n out of space");
else
h->thelist[i]->next=NULL;
}
return h;
}
void insert(int key,ht h,int tablesize)
{
Position p,newcell;
int rem;
list l,r;
p=find(key,h,tablesize);
if(p==NULL);
{
newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)
printf("\n out of space");
else
{
rem=key%tablesize;
l=h->thelist[rem];
newcell->next=l->next;
newcell->ele=key;
l->next=newcell;
}
}
}
void insertline(int key,ht h,int tablesize)
{
int k,fin,inc;
Position p,newcell;
int rem;
list l;
p=find(key,h,tablesize);
fin=0;
if(p==NULL)//
{
newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)
printf("\n out of space");
else
{
rem=key%tablesize;
l=h->thelist[rem];
if(l->next==NULL)
{
newcell->next=l->next;
newcell->ele=key;
l->next=newcell;
}
else
{
k=key;
inc=0;
while(fin==0)
{
inc++;
k=k+1;
rem=k%tablesize;
l=h->thelist[rem];
if(l->next==NULL)
{
newcell->next=l->next;
newcell->ele=key;
l->next=newcell;
fin=1;
}
if(inc==10)
{
printf("\nTable full\n");
fin=1;
}
}
}
}
}
}
Position find(int key,ht h,int tablesize)
{
Position p;
list l;
int rem;
rem=key%tablesize;
l=h->thelist[rem];
p=l->next;
while(p!=NULL && p->ele!=key)
p=p->next;
return p;
}

void display(ht h)
{
int i;
list l;
for(i=0;i<h->tablesize-1;i++)
{
l=h->thelist[i];
printf("\n\t%d:",i);
if(l==NULL)
{
printf(" NULL");
break;
}
while(l!=NULL)
{
printf(" %d->",l->ele);
l=l->next;
}
printf(" NULL");
}
}
int main()
{
int ch=0;
int hs=0;
ht t;
int x;
int tablesize;
printf("Enter the table size:");
scanf("%d",&tablesize);
t=initialise(tablesize);
do
{
printf("Hashing techinques\n");
printf("------------------\n");
printf("\t1.Separate Chaining\n\t2.Linear Probing\n\t3.Exit\nEnter your choice:");
scanf("%d",&hs);
switch(hs)
{
case 1:
do
{
printf("\n MENU\n1.insert\n2.find\n3.display\n4.exit");
printf("\n enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the element");
scanf("%d",&x);
insert(x,t,tablesize);
break;
case 2:printf("\nenter element to be found : ");
scanf("%d",&x);
if(find(x,t,tablesize)!=NULL)
printf("\n%d is present",x);
else
printf("\nelement not found");
break;
case 3:display(t);
break;
case 4:
t=initialise(tablesize);
//makeempty(t);
break;
default:printf("sorry wrong choice");
break;
}
}while(ch<4);
break;
case 2:
do
{
printf("\n MENU\n1.insert\n2.find\n3.display\n4.exit");
printf("\n enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the element");
scanf("%d",&x);
insertline(x,t,tablesize);
break;
case 2:printf("\nenter element to be found : ");
scanf("%d",&x);
if(find(x,t,tablesize)!=NULL)
printf("\n%d is present",x);
else
printf("\nelement not found");
break;
case 3:display(t);
break;
case 4:
t=initialise(tablesize);
//makeempty(t);
break;
default:printf("sorry wrong choice");
break;
}
}while(ch<4);
break;
case 3:
break;
}
}while(hs<3);
}

//int retrive(Position *);

//typedef Position list;

int nextprime(int n)
{
int temp,c,i,c1;
c=1;
for(temp=n+1;c!=0;temp++)
{
c1=1;
for(i=2;i<=(temp/2);i++)
{
if((temp%i)==0)
{
c1=0;
break;
}
}

if(c1!=0)
{
c=0;
break;
}

}
return temp;
}

Das könnte Ihnen auch gefallen