Skip to content

Commit 2065642

Browse files
committed
changes
1 parent 26998e2 commit 2065642

File tree

5 files changed

+155
-161
lines changed

5 files changed

+155
-161
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class HashMap {
2+
private int hsize;
3+
private LinkedList[] buckets;
4+
5+
public HashMap(int hsize) {
6+
buckets = new LinkedList[hsize];
7+
for (int i = 0; i < hsize ; i++ ) {
8+
buckets[i] = new LinkedList();
9+
// Java requires explicit initialisaton of each object
10+
}
11+
this.hsize = hsize;
12+
}
13+
14+
public int hashing(int key) {
15+
int hash = key % hsize;
16+
if(hash < 0)
17+
hash += hsize;
18+
return hash;
19+
}
20+
21+
public void insertHash(int key) {
22+
int hash = hashing(key);
23+
buckets[hash].insert(key);
24+
}
25+
26+
27+
public void deleteHash(int key) {
28+
int hash = hashing(key);
29+
30+
buckets[hash].delete(key);
31+
}
32+
public void displayHashtable() {
33+
for (int i = 0;i < hsize ; i++) {
34+
System.out.printf("Bucket %d :",i);
35+
buckets[i].display();
36+
}
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class LinkedList {
2+
3+
private Node Head;
4+
private int size;
5+
6+
public LinkedList() {
7+
Head = null;
8+
size = 0;
9+
}
10+
11+
public void insert(int data) {
12+
13+
Node temp = Head;
14+
Node newnode = new Node(data);
15+
16+
size++;
17+
18+
if(Head == null) {
19+
Head = newnode;
20+
}
21+
else {
22+
newnode.next = Head;
23+
Head = newnode;
24+
}
25+
}
26+
27+
public void delete(int data) {
28+
if(size == 0) {
29+
System.out.println("UnderFlow!");
30+
return;
31+
}
32+
33+
else {
34+
Node curr = Head;
35+
if (curr.data == data) {
36+
Head = curr.next;
37+
size--;
38+
return;
39+
}
40+
else {
41+
42+
while(curr.next.next != null) {
43+
if(curr.next.data == data){
44+
curr.next = curr.next.next;
45+
return;
46+
}
47+
}
48+
49+
System.out.println("Key not Found");
50+
}
51+
}
52+
}
53+
54+
public void display() {
55+
Node temp = Head;
56+
while(temp != null) {
57+
System.out.printf("%d ",temp.data);
58+
temp = temp.next;
59+
}
60+
System.out.println();
61+
}
62+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
6+
int choice, key;
7+
8+
HashMap h = new HashMap(7);
9+
10+
while (true) {
11+
System.out.println("Enter your Choice :");
12+
System.out.println("1. Add Key");
13+
System.out.println("2. Delete Key");
14+
System.out.println("3. Print Table");
15+
System.out.println("4. Exit");
16+
17+
Scanner In = new Scanner(System.in);
18+
19+
choice = In.nextInt();
20+
21+
switch (choice) {
22+
case 1: {
23+
System.out.println("Enter the Key: ");
24+
key = In.nextInt();
25+
h.insertHash(key);
26+
break;
27+
}
28+
case 2: {
29+
System.out.println("Enter the Key delete: ");
30+
key = In.nextInt();
31+
h.deleteHash(key);
32+
break;
33+
}
34+
case 3: {
35+
System.out.println("Print table");
36+
h.displayHashtable();
37+
break;
38+
}
39+
case 4: {
40+
return;
41+
}
42+
}
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Node {
2+
int data;
3+
Node next;
4+
5+
public Node(int data) {
6+
this.data = data;
7+
this.next = null;
8+
}
9+
}

Data Structures/HashMap/Main.java

-161
This file was deleted.

0 commit comments

Comments
 (0)