forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHashtable_chaining.java
More file actions
166 lines (153 loc) · 4.44 KB
/
Hashtable_chaining.java
File metadata and controls
166 lines (153 loc) · 4.44 KB
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
/*
Hashtable chaining is a collision resolution technique in hashtables
The chaining method uses a hashtable as an array of pointers ; each pointer points a linked list.
For a given key value , the hash address is calculated . It then searches the linked list pointed by
the pointers at that location. If the element is found it returns the pointer to the node containing
that key value else inserts the element at the end of that list.
*/
import java.util.Scanner;
//class for the hashtable
class HashTable {
int data;
HashTable link;
}
// hash function
class Code {
static int hashingFunction(int value) {
return value % 10;
}
// Chaining function inserts an element in the hashtable by hashtable chaining
static void Chaining(HashTable t[], int value) {
int index = hashingFunction(value);
HashTable ptr = t[index];
while (ptr.link != null)
ptr = ptr.link;
HashTable New = new HashTable();
New.data = value;
New.link = null;
ptr.link = New;
}
// print_table function prints the hashtable
static void print_table(HashTable t[]) {
HashTable ptr = t[0];
for (int i = 0; i < 10; i++) {
if (ptr.link != null) {
System.out.print(i + " -> ");
HashTable temp = ptr;
while (temp.link != null) {
temp = temp.link;
System.out.print(temp.data + " ");
}
System.out.println();
}
ptr = t[i];
}
}
// doesExists function searches the hashtable in the chained linked lists
static int does_exist(HashTable t[], int value) {
int index = hashingFunction(value);
HashTable ptr = t[index];
while (ptr.link != null) {
ptr = ptr.link;
if (ptr.data == value)
return 1;
}
return 0;
}
// Main function
public static void main(String[] args) {
HashTable t[] = new HashTable[11];
for (int i = 0; i < 10; i++) {
t[i] = new HashTable();
t[i].link = null;
}
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(
"Enter your choice\n1.insert into hashtable\n2.check whether the element is in the hashtable\n3.print hashtable\n4.Exit");
int choice;
choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the value to be inserted");
int value;
value = sc.nextInt();
Chaining(t, value);
}
break;
case 2: {
System.out.println("Enter the value to check whether the element is in the hashtable");
int value;
value = sc.nextInt();
int exists = does_exist(t, value);
if (exists == 1) {
System.out.println("The element exists in the hashtable");
} else {
System.out.println("The element doesn't exist in the hashtable");
}
}
break;
case 3: {
print_table(t);
}
break;
case 4: {
return;
}
}
}
}
}
/*
Sample I/O:
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
1
Enter the value to be inserted
1
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
1
Enter the value to be inserted
2
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
1
Enter the value to be inserted
3
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
2
Enter the value to check whether the element is in the hashtable
3
The element exists in the hashtable
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
3
2 -> 1
3 -> 2
4 -> 3
Enter your choice
1.insert into hashtable
2.check whether the element is in the hashtable
3.print hashtable
4.Exit
4
Time Complexity : O(n)
Space Complexity : O(n)
*/