Skip to content

Commit 48aeb89

Browse files
Hello
1 parent 7d61835 commit 48aeb89

11 files changed

+643
-0
lines changed

Hash_Folding.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include<stdio.h>
2+
#include<math.h>
3+
#define size 10
4+
5+
int arr[size];
6+
7+
8+
void initialize()
9+
{
10+
int i;
11+
for(i = 0; i < size; i++)
12+
arr[i] = -1;
13+
}
14+
15+
int count_digits(int value){
16+
int c=0;
17+
while(value>0){
18+
c++;
19+
value/=10;
20+
}
21+
return c;
22+
}
23+
24+
int calculate_key(int value){
25+
int key,no_digits=count_digits(value);
26+
if(no_digits<=2){
27+
key=value%size;
28+
}
29+
else{
30+
int copy=value,rev=0;
31+
while(copy>0){
32+
int d=copy%10;
33+
rev=rev*10+d;
34+
copy/=10;
35+
}
36+
int sum=0;
37+
while(rev>0){
38+
int d=rev%100;
39+
int rev_d=0;
40+
while(d>0){
41+
rev_d=rev_d*10 + (d%10);
42+
d/=10;
43+
}
44+
sum+=rev_d;
45+
rev/=100;
46+
}
47+
key = sum%10;
48+
}
49+
return key;
50+
}
51+
52+
void insert(int value)
53+
{
54+
int key=calculate_key(value);
55+
if(arr[key] == -1)
56+
{
57+
arr[key] = value;
58+
}
59+
else
60+
{
61+
int i,pos=key;
62+
for(i=key+1;i<size;i++){
63+
if(arr[i]==-1){
64+
pos=i;
65+
break;
66+
}
67+
}
68+
if(pos!=key)
69+
arr[pos]=value;
70+
else
71+
printf("\nNo free space found. %d cannot be inserted.\n\n",value);
72+
}
73+
}
74+
75+
void search(int value)
76+
{
77+
int key = calculate_key(value);
78+
if(arr[key] == value)
79+
printf("\nSearch Found at position %d\n",key);
80+
else
81+
printf("\nSearch Not Found\n");
82+
}
83+
84+
void display_hash_table()
85+
{
86+
int i;
87+
for(i = 0; i < size; i++)
88+
printf("arr[%d] = %d\n",i,arr[i]);
89+
}
90+
91+
int main()
92+
{
93+
int ins,val;
94+
int conti=1;
95+
96+
initialize();//to initialize the hash table elements to -1
97+
98+
do{
99+
printf("Press 1 to insert element\n");
100+
printf("Press 2 to search for an element\n");
101+
printf("Press 3 to display the hash table\n");
102+
printf("Press 4 to exit\n");
103+
int ch;
104+
printf("Enter choice : ");
105+
scanf("%d",&ch);
106+
switch(ch){
107+
case 1:
108+
printf("Enter element to be inserted : ");
109+
scanf("%d",&ins);
110+
insert(ins);
111+
break;
112+
113+
case 2:
114+
printf("Enter element to be searched for : ");
115+
scanf("%d",&val);
116+
search(val);
117+
break;
118+
119+
case 3:
120+
printf("Hash Table elements are : \n");
121+
display_hash_table();
122+
break;
123+
124+
case 4:
125+
conti=0;
126+
break;
127+
128+
default:printf("WRONG CHOICE\n");
129+
}
130+
}while(conti==1);
131+
return 0;
132+
}

Hash_Folding.exe

42.2 KB
Binary file not shown.

Hash_Table1.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include<stdio.h>
2+
3+
#define size 10
4+
5+
int arr[size];
6+
7+
8+
void initialize()
9+
{
10+
int i;
11+
for(i = 0; i < size; i++)
12+
arr[i] = -1;
13+
}
14+
15+
void insert(int value)
16+
{
17+
int key = value % size;
18+
19+
if(arr[key] == -1)
20+
{
21+
arr[key] = value;
22+
}
23+
else
24+
{
25+
int i,pos=key;
26+
for(i=key+1;i<size;i++){
27+
if(arr[i]==-1){
28+
pos=i;
29+
break;
30+
}
31+
}
32+
if(pos!=key)
33+
arr[pos]=value;
34+
else
35+
printf("No free space found. %d cannot be inserted.\n\n",value);
36+
}
37+
}
38+
39+
void search(int value)
40+
{
41+
int key = value % size;
42+
if(arr[key] == value)
43+
printf("Search Found\n");
44+
else
45+
printf("Search Not Found\n");
46+
}
47+
48+
void display_hash_table()
49+
{
50+
int i;
51+
for(i = 0; i < size; i++)
52+
printf("arr[%d] = %d\n",i,arr[i]);
53+
}
54+
55+
int main()
56+
{
57+
int ins,val;
58+
int conti=1;
59+
60+
initialize();//to initialize the hash table elements to -1
61+
62+
do{
63+
printf("Press 1 to insert element\n");
64+
printf("Press 2 to search for an element\n");
65+
printf("Press 3 to display the hash table\n");
66+
printf("Press 4 to exit\n");
67+
int ch;
68+
printf("Enter choice : ");
69+
scanf("%d",&ch);
70+
switch(ch){
71+
case 1:
72+
printf("Enter element to be inserted : ");
73+
scanf("%d",&ins);
74+
insert(ins);
75+
break;
76+
77+
case 2:
78+
printf("Enter element to be searched for : ");
79+
scanf("%d",&val);
80+
search(val);
81+
break;
82+
83+
case 3:
84+
printf("Hash Table elements are : \n");
85+
display_hash_table();
86+
break;
87+
88+
case 4:
89+
conti=0;
90+
break;
91+
92+
default:printf("WRONG CHOICE\n");
93+
}
94+
}while(conti==1);
95+
return 0;
96+
}

Hash_Table1.exe

42.1 KB
Binary file not shown.

Hash_Table2.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<stdio.h>
2+
#include<math.h>
3+
#define size 10
4+
5+
int arr[size];
6+
7+
8+
void initialize()
9+
{
10+
int i;
11+
for(i = 0; i < size; i++)
12+
arr[i] = -1;
13+
}
14+
15+
void insert(int value)
16+
{
17+
int key =size*remainder((value*0.2),1);
18+
19+
if(arr[key] == -1)
20+
{
21+
arr[key] = value;
22+
}
23+
else
24+
{
25+
int i,pos=key;
26+
for(i=key+1;i<size;i++){
27+
if(arr[i]==-1){
28+
pos=i;
29+
break;
30+
}
31+
}
32+
if(pos!=key)
33+
arr[pos]=value;
34+
else
35+
printf("No free space found. %d cannot be inserted.\n\n",value);
36+
}
37+
}
38+
39+
void search(int value)
40+
{
41+
int key = size*remainder((value*0.2),1);
42+
if(arr[key] == value)
43+
printf("\nSearch Found at position %d\n",key);
44+
else
45+
{ int flag=0,pos;
46+
for(int i=key+1;i<size;i++){
47+
if(arr[i]==value){
48+
flag=1;
49+
pos=i;
50+
break;
51+
}
52+
}
53+
if(flag==1)
54+
printf("\nSearch found at position %d\n",pos);
55+
else
56+
printf("\nSearch not found\n");
57+
}
58+
}
59+
60+
void display_hash_table()
61+
{
62+
int i;
63+
for(i = 0; i < size; i++)
64+
printf("arr[%d] = %d\n",i,arr[i]);
65+
}
66+
67+
int main()
68+
{
69+
int ins,val;
70+
int conti=1;
71+
72+
initialize();//to initialize the hash table elements to -1
73+
74+
do{
75+
printf("Press 1 to insert element\n");
76+
printf("Press 2 to search for an element\n");
77+
printf("Press 3 to display the hash table\n");
78+
printf("Press 4 to exit\n");
79+
int ch;
80+
printf("Enter choice : ");
81+
scanf("%d",&ch);
82+
switch(ch){
83+
case 1:
84+
printf("Enter element to be inserted : ");
85+
scanf("%d",&ins);
86+
insert(ins);
87+
break;
88+
89+
case 2:
90+
printf("Enter element to be searched for : ");
91+
scanf("%d",&val);
92+
search(val);
93+
break;
94+
95+
case 3:
96+
printf("Hash Table elements are : \n");
97+
display_hash_table();
98+
break;
99+
100+
case 4:
101+
conti=0;
102+
break;
103+
104+
default:printf("WRONG CHOICE\n");
105+
}
106+
}while(conti==1);
107+
return 0;
108+
}

Hash_Table2.exe

42.1 KB
Binary file not shown.

Pattern_Aratrika.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)