Practical DS
Practical DS
#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 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;
* 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
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};
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
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
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
_______________________________________________________________________