1+ import java .util .ArrayList ;
2+
3+ import java .util .Arrays ;
4+
5+ import java .util .Random ;
6+
7+ import java .util .LinkedList ;
8+
9+ import java .util .Scanner ;
10+
11+ class StudentRecord {
12+
13+ private String Id ;
14+
15+ private StudentRecord next ;
16+
17+ StudentRecord (String inputId ){
18+
19+ Id = inputId ;
20+
21+ next = null ;
22+
23+ }
24+
25+ public StudentRecord getNext (){
26+
27+ return next ;
28+
29+ }
30+
31+ public void setNext (StudentRecord nextRecord ){
32+
33+ }
34+
35+ public String getId (){
36+
37+ }
38+
39+ };
40+
41+ public class Hashing {
42+
43+ StudentRecord [] studentArray ;
44+
45+ static int sizeofthearray ;
46+
47+ next = nextRecord ;
48+
49+ return Id ;
50+
51+ static int hashNumber ;
52+
53+ Hashing (int size ) {
54+
55+ sizeofthearray = size ;
56+
57+ studentArray = new StudentRecord [size ];
58+
59+ for (int i =0 ; i < sizeofthearray ;i ++){
60+
61+ studentArray [i ]= null ;
62+
63+ }
64+
65+ }
66+
67+ public void put (String Id ){
68+
69+ int hash = getHashValueFromDiv (Id );
70+
71+ if (studentArray [hash ] == null )
72+
73+ studentArray [hash ] = new StudentRecord (Id );
74+
75+ else {
76+
77+ StudentRecord record = studentArray [hash ];
78+
79+ while (record .getNext () != null )
80+
81+ record = record .getNext ();
82+
83+ record .setNext (new StudentRecord (Id ));
84+
85+ }
86+
87+ }
88+
89+
90+
91+ public int getHashValueFromDiv (String input ){
92+
93+ return Integer .parseInt (input ) % hashNumber ;
94+
95+ }
96+
97+ public void listDisplay (){
98+
99+ for (int i =0 ; i <sizeofthearray ; i ++){
100+
101+ StudentRecord temp = studentArray [i ];
102+
103+ if (temp == null ){
104+
105+ System .out .println (i +"--> empty" );
106+
107+ }
108+
109+ else {
110+
111+ System .out .print (i +" --> " +temp .getId ()
112+
113+ +" " );
114+
115+ temp =temp .getNext ();
116+
117+ while (temp != null ){
118+
119+ System .out .print (temp .getId ()+" " );
120+
121+ temp = temp .getNext ();
122+
123+ }
124+
125+ System .out .println ();
126+
127+ }
128+
129+ }
130+
131+ }
132+
133+ public static void getHashNumber (){
134+
135+ for (int i = 50 ; i <64 ; i ++) {
136+
137+ if (checkprime (i )){
138+
139+ hashNumber =i ;
140+
141+ }
142+
143+ break ;
144+
145+ sizeofthearray = hashNumber ;
146+
147+ }
148+
149+ }
150+
151+
152+
153+ public static boolean checkprime (int number ) {
154+
155+ if (number % 2 == 0 )
156+
157+ return false ;
158+
159+ for (int i = 3 ; i * i <= number ; i += 2 ) {
160+
161+ if (number % i == 0 )
162+
163+ }
164+
165+ return true ;
166+
167+ return false ;
168+
169+ }
170+
171+ public int findId (String Id ) {
172+
173+ int hash = getHashValueFromDiv (Id );
174+
175+
176+
177+ if (studentArray [hash ] == null )
178+
179+ return -1 ;
180+
181+ else {
182+
183+ StudentRecord entry = studentArray [hash ];
184+
185+ while (entry != null && !(entry .getId ().equals (Id )
186+
187+ ))
188+
189+ entry = entry .getNext ();
190+
191+ if (entry == null )
192+
193+ return -1 ;
194+
195+ else
196+
197+ return Integer .parseInt (entry .getId ());
198+
199+ }
200+
201+ }
202+
203+ public static void main (String [] args ) {
204+
205+ getHashNumber ();
206+
207+ 900000 );
208+
209+ Hashing hash = new Hashing (hashNumber );
210+
211+ Random rand = new Random ();
212+
213+ int arr [] = new int [160 ];
214+
215+ for (int i = 0 ; i < 160 ; i ++)
216+
217+ {
218+
219+ arr [i ] = 100000 + (int )(rand .nextFloat () *
220+
221+ }
222+
223+ String [] displayelements = new
224+
225+ String [arr .length ];
226+
227+ for (int j = 0 ; j < arr .length ; j ++)
228+
229+ {
230+
231+ displayelements [j ]=String .valueOf (arr [j ]);
232+
233+ hash .put (displayelements [j ]);
234+
235+ }
236+
237+ hash .listDisplay ();
238+
239+ Scanner reader = new Scanner (System .in );
240+
241+ int i =0 ;
242+
243+ while (i <=2 )
244+
245+ {
246+
247+ System .out .println ("Enter the Id to be
248+
249+ searched" );
250+
251+ String searchKey =reader .next ();
252+
253+
254+
255+ if (hash .findId (searchKey ) > 0 ){
256+
257+ System .out .println ("Id found in the hash
258+
259+ table" );
260+
261+ }
262+
263+ else {
264+
265+ System .out .println ("Id not found" );
266+
267+
268+
269+ }
270+
271+ }
272+
273+ }
274+
275+ }
0 commit comments