-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
187 lines (161 loc) · 7.29 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//This is the main program for testing the HASH-TABLE functions
# include "hash.h"
int main()
{
int k,a,b,n;
ll_node *t1;
bst_node *t2;
ll_hashtable h1=NULL;
bst_hashtable h2=NULL;
system("clear");
printf("\t\t\tWELCOME TO THE WORLD OF HASHTABLE\n");
printf("\t\t\t--------------------------------------\n");
printf("\tIn this,the user can do following by choosing either linked list or binary search tree for the hashtable:\n");
printf("1)Creating a hashtable by generating data in a file\n");
printf("2)Searching a key in the hashtable\n");
printf("3)Deleting a key in the hashtable\n4)Destroying the hashtable\n");
printf("5)Finding the length or depth of a particular bucket\n");
printf("6)Comparing both linked list & binary search trees performance by their lengths and depths\n");
printf("\nNOTE : In this,'key' is an integer and 'value' is a string\n");
printf("\nSo,now you can use the hashtable--------->\n\n");
printf("Do u want to construct the hashtable by linked list or binary search tree\n");
printf("If linked list,enter '1' or if binary search tree,enter '2' :\n");
printf("OR if u want to compare both performance,enter '3'");
scanf("%d",&b);
if(b==1)
while(1)
{
printf("\n\nChoose the appropriate option of ur choice from the following :\n");
printf("1)Generation of data in a file & insertion of data in a hashtable\n");
printf("2)Searching a key(integer) in the hashtable\n");
printf("3)Deleting a key(integer) in the hashtable\n");
printf("4)Length of a bucket in the hashtable\n");
printf("5)Destroying the HASHTABLE\n");
printf("6)Exiting the program\n");
scanf("%d",&a);
switch(a)
{
case 1 : printf("Enter number of keys to be generated :\n");
scanf("%d",&k);
h1=hashing_by_lists(h1,k); //generates the data & inserts in the hash table
ll_output(h1); //writes the hashtable in "out.dat" file
break;
case 2 : if(h1==NULL)
printf("SORRY!!!Hashtable is empty\n");
else
{
printf("Enter a key to search in the hashtable: \n");
scanf("%d",&k);
t1=ll_look_up(k,h1); //searches the key 'k'
if(t1==NULL)
printf("The given key is not there in the hashtable\n");
else
printf("KEY---->%d\nVALUE---->%s\n",t1->key,t1->value);
}
break;
case 3 : if(h1==NULL)
printf("Sorry!!!HASHTABLE is empty\n");
else
{
printf("Enter a key to delete from the hashtable: \n");
scanf("%d",&k);
h1=ll_delet(k,h1); //deletes the key k from hashtable
ll_output(h1); //removes the key from 'out.dat' file
}
break;
case 4 : if(h1==NULL)
printf("Sorry!!!HASHTABLE is empty\n");
else
{
printf("Enter a bucket no. to find it's depth: \n");
scanf("%d",&k);
n=length(h1,k);
printf("The length of '%d' bucket is '%d'\n",k,n);
}
break;
case 5 : if(h1!=NULL)
ll_destroy(h1);
else
printf("HASHTABLE is already empty\n");
break;
case 6 : exit(0); //exits the program
break;
default: printf("SORRY !!! This case is not there\n");
break;
}//End of switch case
}//End of while loop
if(b==2)
while(1)
{
printf("\n\nChoose the appropriate option of ur choice from the following :\n");
printf("1)Generation of data in a file & insertion of data in a hashtable\n");
printf("2)Searching a key(integer) in the hashtable\n");
printf("3)Deleting a key(integer) in the hashtable\n");
printf("4)Depth of a bucket in the hashtable\n");
printf("5)Exiting the program\n");
scanf("%d",&a);
switch(a)
{
case 1 : printf("Enter number of keys to be generated :\n");
scanf("%d",&k);
h2=hashing_by_bst(h2,k); //generates the data & inserts in the hash table
bst_output(h2); //writes the hashtable in "out.dat" file
break;
case 2 : if(h2==NULL)
printf("SORRY!!!Hashtable is empty\n");
else
{
printf("Enter a key to search in the hashtable: \n");
scanf("%d",&k);
retrieve(k,h2); //searches the key 'k'
}
break;
case 3 : if(h2==NULL)
printf("SORRY!!!Hashtable is empty\n");
else
{
printf("Enter a key to delete from the hashtable: \n");
scanf("%d",&k);
h2=delete_a_key(k,h2); //deletes the key k from hashtable
bst_output(h2); //removes the key from 'out.dat' file
}
break;
case 4 : if(h2==NULL)
printf("SORRY!!!Hashtable is empty\n");
else
{
printf("Enter a bucket no. to find it's depth: \n");
scanf("%d",&k);
t2=h2->lists[k];
n=depth(t2,0);
printf("The depth of '%d' bucket is '%d'\n",k,n);
}
break;
case 5 : exit(0); //exits the program
break;
default: printf("SORRY !!! This case is not there\n");
break;
}//End of switch case
}//End of while loop
if(b==3)
{
printf("Enter number of keys to be generated :\n");
scanf("%d",&k);
h1=hashing_by_lists(h1,k); //generates the data & inserts in the hash table
ll_output(h1); //writes the hashtable in "out.dat" file
printf("\nGive the same input as given above\n\n");
h2=hashing_by_bst(h2,k); //generates the data & inserts in the hash table
bst_output(h2); //writes the hashtable in "out.dat" file
while(h1->tblsize!=h2->tblsize)
{
printf("SORRY!U have given wrong tablesize \n");
printf("PLEASE! Give the table size as given above \n");
h2=hashing_by_bst(h2,k); //generates the data & inserts in the hash table
bst_output(h2); //writes the hashtable in "out.dat" file
}
compare(h1,h2);
printf("Now,open the file 'compare.dat' for seeing the comparision\n");
}
else;
return 0;
}//End of main