0% found this document useful (0 votes)
8 views8 pages

Practical DS

The document contains two practical programming exercises focused on hash table implementations in C. The first exercise demonstrates three hashing methods (division, mid-square, and folding) with functionalities for insertion, deletion, searching, and displaying the hash table. The second exercise implements a hash table using linked lists to handle collisions, providing similar functionalities for inserting, searching, and deleting keys.

Uploaded by

Jay Ware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Practical DS

The document contains two practical programming exercises focused on hash table implementations in C. The first exercise demonstrates three hashing methods (division, mid-square, and folding) with functionalities for insertion, deletion, searching, and displaying the hash table. The second exercise implements a hash table using linked lists to handle collisions, providing similar functionalities for inserting, searching, and deleting keys.

Uploaded by

Jay Ware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

* PRACTICAL NUMBER :- 7 *

#include<stdio.h>
#include<conio.h>
#define size 10
int ht[size];
void init()
{
int i;
for(i=0;i<size;i++)
ht[i]=-1;
}
void division_method(int value)
{
int key = value % size;
if(ht[key]==-1)
{
ht[key] = value;
printf("%d inserted at ht[%d] \n",value,key);
}
else
{
printf("Collision : ht[%d] has element %d already! \n",key, ht[key]);
printf("Unable to insert %d\n",value);
}
}

void mid_square(int value)


{
int square = value*value;
int mid = square /100 % 100;
int key = mid % size;
if(ht[key]==-1)
{
ht[key] = value;
printf("%d inserted at ht[%d] \n",value,key);
}
else
{
printf("Collision : ht[%d] has element %d already! \n",key, ht[key]);
printf("Unable to insert %d\n",value);
}
}

void folding(int value)


{
int sum = 0,key,value1=value;
while(value>0)
{
sum+=value%10;
value/=10;
}
key = sum % size;
if(ht[key]==-1)
{
ht[key] = value1;
printf("%d inserted at ht[%d] \n",value1,key);
}
else
{
printf("Collision : ht[%d] has element %d already! \n",key, ht[key]);
printf("Unable to insert %d\n",value);
}
}
void del(int value)
{
int key = value % size;
if(ht[key] == value)
ht[key] = -1;
else
printf("%d not present in the hash table \n",value);
}

void search(int value)


{
int key = value % size;
if(ht[key] == value)
printf("%d is present at position ht[%d] in hash table\n",value,key);
else
printf("%d not present in the hash table\n",value);
}

void print()
{
int i;
for(i=0;i<size;i++)
printf("ht[%d] = %d\n",i,ht[i]);
}

void main()
{
int n, value,i,ch;
clrscr();
init();
do
{
printf("\n1: Division method \n2: Mid square method \n3: Folding
method");
printf("\n4: Display \n5: Search \n6: Delete \n7: Exit");
printf("\nEnter your choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1: //Division Method
printf("How many values you wants to enter = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element = ");
scanf("%d",&value);
division_method(value);
}
break;

case 2: //Mid Square Method


printf("How many values you wants to enter = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element = ");
scanf("%d",&value);
mid_square(value);
}
break;

case 3: //Folding Method


printf("How many values you wants to enter = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element = ");
scanf("%d",&value);
folding(value);
}
break;

case 4: //Display Hash Table


printf("\n");
print();
break;

case 5: //Search the key


printf("Enter the element to search = ");
scanf("%d",&value);
search(value);
break;

case 6: //Delete the key


printf("Enter the element to delete = ");
scanf("%d",&value);
del(value);
break;
}
}while(ch!=7);
getch();
}

* Output :-
1: Division method
2: Mid square method
3: Folding method
4: Display
5: Search
6: Delete
7: Exit
Enter your choice = 1
How many values you wants to enter = 6

Enter the element = 15


15 inserted at ht[5]

Enter the element = 19


19 inserted at ht[9]

Enter the element = 23


23 inserted at ht[3]

Enter the element = 8


8 inserted at ht[8]

Enter the element = 34


34 inserted at ht[4]

Enter the element = 70


70 inserted at ht[0]

1: Division method
2: Mid square method
3: Folding method
4: Display
5: Search
6: Delete
7: Exit
Enter your choice = 4

ht[0] = 70
ht[1] = -1
ht[2] = -1
ht[3] = 23
ht[4] = 34
ht[5] = 15
ht[6] = -1
ht[7] = -1
ht[8] = 8
ht[9] = 19

1: Division method
2: Mid square method
3: Folding method
4: Display
5: Search
6: Delete
7: Exit
Enter your choice = 5
Enter the element to search = 19
19 is present at position ht[9] in hash table

1: Division method
2: Mid square method
3: Folding method
4: Display
5: Search
6: Delete
7: Exit
Enter your choice = 7
* PRACTICAL NUMBER :- 8 *
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define size 10
typedef struct node
{
int key;
struct node *next;
}node;

node * ht[size]={NULL};

int hf(int key)


{
return (key%size);
}
void insert(int k)
{
int index;
node * newnode=NULL, *temp;
newnode=(node *)malloc(sizeof(node));
newnode->key=k;
newnode->next=NULL;
index=hf(k);
if(ht[index]==NULL)
ht[index]=newnode;
else
{
temp=ht[index];
while(temp->next!= NULL)
temp = temp->next;
temp->next=newnode;
}
}

void search(int key)


{
int index;
node *temp;
index = hf(key);
for(temp=ht[index];temp!=NULL;temp=temp->next)
if(temp->key==key)
{
printf("%d found\n",key);
return;
}
printf("%d not found\n",key);
}

void deletekey(int key)


{
int index;
node * temp;
index = hf(key);
temp = ht[index];
if((temp!=NULL)&&(temp->key==key))
ht[index] = temp->next;
else
{
for(temp=ht[index]; temp!=NULL; temp=temp->next)
if(temp->next->key==key)
{
temp->next=temp->next->next;
return;
}
printf("%d not found\n",key);
}
}

void showTable()
{
int i, index;
node * temp;
for(i=0;i<size;i++)
{
printf("%d |",i);
for(temp=ht[i];temp!=NULL;temp=temp->next)
printf("%d ->",temp->key);
printf("NULL\n");
}
}

void main()
{
int ch,n,i,key;
clrscr();

do
{
printf("\n1: Insert \n2: Search \n3: Delete \n4: Display \n5: Exit");
printf("\nEnter your choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1: //insert
printf("\nHow many element you want to enter = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the value = ");
scanf("%d",&key);
insert(key);
}
break;

case 2: //search
printf("\nEnter the value to search = ");
scanf("%d",&key);
search(key);
break;

case 3: //delete
printf("\nEnter the value to delete = ");
scanf("%d",&key);
deletekey(key);
break;

case 4: //display
showTable();
break;
}
}while(ch!=5);
}

* Output :-

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 1

How many element you want to enter = 6

Enter the value = 15

Enter the value = 19

Enter the value = 23

Enter the value = 8

Enter the value = 9

Enter the value = 34

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 4

0 |NULL
1 |NULL
2 |NULL
3 |23 ->NULL
4 |34 ->NULL
5 |15 ->NULL
6 |NULL
7 |NULL
8 |8 ->NULL
9 |19 ->9 ->NULL

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 2
Enter the value to search = 19
19 found

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 3

Enter the value to delete = 9

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 4

0 |NULL
1 |NULL
2 |NULL
3 |23 ->NULL
4 |34 ->NULL
5 |15 ->NULL
6 |NULL
7 |NULL
8 |8 ->NULL
9 |19 ->NULL

1: Insert
2: Search
3: Delete
4: Display
5: Exit
Enter your choice = 5

_______________________________________________________________________

You might also like