program For EISCH //name - Jatin Chand Deopa //ID - 51944

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

//Program for EISCH

//Name - Jatin Chand Deopa


//ID - 51944
#include<stdio.h>
#include<stdlib.h>
struct element
{
int data;
struct element *link;
};
typedef struct element record;
record *r;
record *r1; // pointer to last memory space of array
// To intialize all values to -1 and pointer to NULL
record *table(int n)
{
record *arr=(record*)malloc(n * sizeof(record));
record *ptr1=arr;
for(int i=0; i<n; i++)
{
ptr1->data=-1;
ptr1->link=NULL;
ptr1++;
}
return arr;
}

int hashing(int key, record *ptr, int n)


{
int pos=key%n;
record *temp1;
if(ptr[pos].data==-1)
ptr[pos].data=key;
else if(ptr[pos].data!=-1&&ptr[pos].link==NULL)
{
r--;
while(r->data!=-1)
r--;
r->data=key;
ptr[pos].link=r;
}
else if(ptr[pos].data!=-1&&ptr[pos].link!=NULL)
{
temp1=ptr[pos].link;
r--;
while(r->data!=-1)
r--;
r->data=key;
r->link=temp1;
ptr[pos].link=r;
}
}

int searching(int search, record *ptr, int n) //Function for searching an element in table
{
int pos=search%n;
record *temp2;
if(ptr[pos].data==search)
return pos+1;
else if(ptr[pos].data!=search&&ptr[pos].link==NULL)
return -1;
else if(ptr[pos].data!=search&&ptr[pos].link!=NULL)
{
temp2=ptr[pos].link;
while(temp2->link!=NULL)
{
if(temp2->data==search)
{
return (temp2-ptr)+1;
}
temp2=temp2->link;
}
return (temp2-ptr)+1;
}
}

int display(record *ptr, int n) //Function to display all the table elements
{
printf("Entered elements are:-\n");
for(int i=0; i<n; i++)
{
printf("%d ",ptr->data);
printf("%d\n", ptr->link);
ptr++;
}
}
int main ()
{ int n, num,key,search,i;
int val,pos;
printf("Please enter the value of n for hash function\n");
scanf("%d", &n);
record *ptr=table(n);
r=ptr+n;
printf("Please enter the number of elements to be inserted\n");
scanf("%d",&num);
printf("Now enter the table elements\n");
for(int i=0; i<num; i++)
{
scanf("%d",&key);
hashing(key, ptr, n);
}
printf("\nThe elements are: ");
display(ptr,n);

do
{
printf("\n1.Search the element");
printf("\n2.Exit");
printf("\nSelect the option\n");
scanf("%d",&val);
switch(val)
{

case 1 : printf("\nEnter the no. to be searched\n");


scanf("%d", &search);
pos=searching(search, ptr, n);
if(pos==-1)
printf("Element not found\n");
else
printf("Element found at position %d", pos);
}
}while(val!=2);
return 0;
}
//Program for LICH
//Name - Jatin Chand Deopa
//ID - 51944
#include<stdio.h>
#include<stdlib.h>
struct element
{
int data;
struct element *link;
};
typedef struct element record;
record *r;
record *r1; // pointer to last memory space of array
// To intialize all values to -1 and pointer to NULL
record *table(int n)
{
record *arr=(record*)malloc(n * sizeof(record));
record *ptr1=arr;
for(int i=0; i<n; i++)
{
ptr1->data=-1;
ptr1->link=NULL;
ptr1++;
}
return arr;
}

int hashing(int key, record *ptr, int n, int cellar) //Function to insert elements
{
int pos=key%(n-cellar);
record *temp1;
if(ptr[pos].data==-1)
ptr[pos].data=key;
else if(ptr[pos].data!=-1&&ptr[pos].link==NULL)
{
r--;
while(r->data!=-1)
r--;
r->data=key;
ptr[pos].link=r;
}
else if(ptr[pos].data!=-1&&ptr[pos].link!=NULL)
{
temp1=ptr[pos].link;
while(temp1->link!=NULL)
temp1=temp1->link;
r--;
while(r->data!=-1)
r--;
r->data=key;
temp1->link=r;
}
}

int searching(int search, record *ptr, int n, int cellar) //Function for searching an element in table
{
int pos=search%(n-cellar);
record *temp2;
if(ptr[pos].data==search)
return pos+1;
else if(ptr[pos].data!=search&&ptr[pos].link==NULL)
return -1;
else if(ptr[pos].data!=search&&ptr[pos].link!=NULL)
{
temp2=ptr[pos].link;
while(temp2->link!=NULL)
{
if(temp2->data==search)
{
return (temp2-ptr)+1;
}
temp2=temp2->link;
}
return (temp2-ptr)+1;
}
}
int display(record *ptr, int n) //Function to display all the table elements
{
printf("Entered elements are:-\n");
for(int i=0; i<n; i++)
{
printf("%d ",ptr->data);
ptr++;
}
}
int main ()
{ int n, num,key,search,i;
int val,pos;
printf("Please enter the value of n for hash function\n");
scanf("%d",&n);
int cellar;
scanf("%d",&cellar);
record *ptr=table(n);
r=ptr+n;
printf("Please enter the number of elements to be inserted\n");
scanf("%d",&num);
printf("Now enter the table elements\n");
for(int i=0; i<num; i++)
{
scanf("%d",&key);
hashing(key, ptr, n,cellar);
}
printf("\nThe elements are: ");
display(ptr,n);

do
{
printf("\n1.Search the element");
printf("\n2.Exit");
printf("\nSelect the option\n");
scanf("%d",&val);
switch(val)
{

case 1 : printf("\nEnter the no. to be searched\n");


scanf("%d", &search);
pos=searching(search, ptr, n,cellar);
if(pos==-1)
printf("Element not found\n");
else
printf("Element found at position %d", pos);
}
}while(val!=2);
return 0;
}

You might also like