1
+ import java .util .Scanner ;
2
+
3
+ class Node {
4
+ int data ;
5
+ Node next ;
6
+
7
+ public Node (int data ) {
8
+ this .data = data ;
9
+ this .next = null ;
10
+ }
11
+ }
12
+
13
+ class LinkedList {
14
+
15
+ private Node Head ;
16
+ private int size ;
17
+
18
+ public LinkedList () {
19
+ Head = null ;
20
+ size = 0 ;
21
+ }
22
+
23
+ public void insert (int data ) {
24
+
25
+ Node temp = Head ;
26
+ Node newnode = new Node (data );
27
+
28
+ size ++;
29
+
30
+ if (Head == null ) {
31
+ Head = newnode ;
32
+ }
33
+ else {
34
+ newnode .next = Head ;
35
+ Head = newnode ;
36
+ }
37
+ }
38
+
39
+ public void delete (int data ) {
40
+ if (size == 0 ) {
41
+ System .out .println ("UnderFlow!" );
42
+ return ;
43
+ }
44
+
45
+ else {
46
+ Node curr = Head ;
47
+ if (curr .data == data ) {
48
+ Head = curr .next ;
49
+ size --;
50
+ return ;
51
+ }
52
+ else {
53
+
54
+ while (curr .next .next != null ) {
55
+ if (curr .next .data == data ){
56
+ curr .next = curr .next .next ;
57
+ return ;
58
+ }
59
+ }
60
+
61
+ System .out .println ("Key not Found" );
62
+ }
63
+ }
64
+ }
65
+
66
+ public void display () {
67
+ Node temp = Head ;
68
+ while (temp != null ) {
69
+ System .out .printf ("%d " ,temp .data );
70
+ temp = temp .next ;
71
+ }
72
+ System .out .println ();
73
+ }
74
+ }
75
+
76
+
77
+ class HashMap {
78
+ private int hsize ;
79
+ private LinkedList [] buckets ;
80
+
81
+ public HashMap (int hsize ) {
82
+ buckets = new LinkedList [hsize ];
83
+ for (int i = 0 ; i < hsize ; i ++ ) {
84
+ buckets [i ] = new LinkedList ();
85
+ // Java requires explicit initialisaton of each object
86
+ }
87
+ this .hsize = hsize ;
88
+ }
89
+
90
+ public int hashing (int key ) {
91
+ int hash = key % hsize ;
92
+ if (hash < 0 )
93
+ hash += hsize ;
94
+ return hash ;
95
+ }
96
+
97
+ public void insertHash (int key ) {
98
+ int hash = hashing (key );
99
+ //System.out.println(hash);
100
+ buckets [hash ].insert (key );
101
+ }
102
+
103
+
104
+ public void deleteHash (int key ) {
105
+ int hash = hashing (key );
106
+
107
+ buckets [hash ].delete (key );
108
+ }
109
+ public void displayHashtable () {
110
+ for (int i = 0 ;i < hsize ; i ++) {
111
+ System .out .printf ("Bucket %d :" ,i );
112
+ buckets [i ].display ();
113
+ }
114
+ }
115
+
116
+ }
117
+
118
+ public class Main {
119
+ public static void main (String [] args ) {
120
+
121
+ int choice , key ;
122
+
123
+ HashMap h = new HashMap (7 );
124
+
125
+
126
+ while (true ) {
127
+ System .out .println ("Enter your Choice :" );
128
+ System .out .println ("1. Add Key" );
129
+ System .out .println ("2. Delete Key :" );
130
+ System .out .println ("3. Print Table" );
131
+ System .out .println ("4. Exit" );
132
+
133
+ Scanner In = new Scanner (System .in );
134
+
135
+ choice = In .nextInt ();
136
+
137
+ switch (choice ) {
138
+ case 1 :{
139
+ System .out .println ("Enter the Key: " );
140
+ key = In .nextInt ();
141
+ h .insertHash (key );
142
+ break ;
143
+ }
144
+ case 2 : {
145
+ System .out .println ("Enter the Key delete: " );
146
+ key = In .nextInt ();
147
+ h .deleteHash (key );
148
+ break ;
149
+ }
150
+ case 3 : {
151
+ System .out .println ("Print table" );
152
+ h .displayHashtable ();
153
+ break ;
154
+ }
155
+ case 4 : {
156
+ return ;
157
+ }
158
+ }
159
+ }
160
+ }
161
+ }
0 commit comments