fork download
  1.  
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class hashMap{
  9. int size;
  10. boolean state[];
  11. int map[],fre[];
  12.  
  13. hashMap(int size){
  14. this.size=size;
  15. map=new int[size];
  16. fre=new int[size];
  17. state=new boolean[size];
  18. }
  19. int hashValue(int key){
  20. return key%size;
  21. }
  22. void insert(int key){
  23. int index=hashValue(key);
  24. int start=index;
  25. while(true){
  26. if(!state[index]){
  27. map[index]=key;
  28. fre[index]=1;
  29. state[index] = true;
  30. System.out.println("Key Inserted");
  31. return;
  32. }
  33. else if(map[index]==key){
  34. fre[index]++;
  35. System.out.println("Key Inserted");
  36. return;
  37. }
  38. index=(index+1)%size;
  39. if(index==start){
  40. System.out.println("HashTable is Full");
  41. return;
  42. }
  43. }
  44. }
  45. void delete(int key){
  46. int index=hashValue(key);
  47. int start=index;
  48. while(true){
  49. if(state[index] && map[index]==key){
  50. state[index]=false;
  51. System.out.println("Key Deleted");
  52. return;
  53. }
  54. index=(index+1)%size;
  55. if(index==start){
  56. break;
  57. }
  58. }
  59. System.out.println("Element not found");
  60. }
  61. int search(int key){
  62. int index=hashValue(key);
  63. int start=index;
  64. while(true){
  65. if(state[index] && map[index]==key){
  66. return index;
  67. }
  68. index=(index+1)%size;
  69. if(index==start) break;
  70. }
  71. return -1;
  72. }
  73. }
  74. public class Main {
  75. public static void main(String[] args) {
  76. Scanner sc = new Scanner(System.in);
  77.  
  78. System.out.print("Enter hash map size: ");
  79. int size = sc.nextInt();
  80.  
  81. hashMap map = new hashMap(size);
  82.  
  83. while (true) {
  84. System.out.println("\nChoose Operation:");
  85. System.out.println("1. Insert");
  86. System.out.println("2. Delete");
  87. System.out.println("3. Search");
  88. System.out.println("4. Exit");
  89. System.out.print("Enter choice: ");
  90. int choice = sc.nextInt();
  91.  
  92. switch (choice) {
  93. case 1:
  94. System.out.print("Enter key to insert: ");
  95. int insertKey = sc.nextInt();
  96. map.insert(insertKey);
  97. break;
  98.  
  99. case 2:
  100. System.out.print("Enter key to delete: ");
  101. int deleteKey = sc.nextInt();
  102. map.delete(deleteKey);
  103. break;
  104.  
  105. case 3:
  106. System.out.print("Enter key to search: ");
  107. int searchKey = sc.nextInt();
  108. int index = map.search(searchKey);
  109. if (index != -1) {
  110. System.out.println("Key found at index: " + index);
  111. } else {
  112. System.out.println("Key not found.");
  113. }
  114. break;
  115.  
  116. case 4:
  117. System.out.println("Exiting...");
  118. sc.close();
  119. return;
  120.  
  121. default:
  122. System.out.println("Invalid choice.");
  123. }
  124. }
  125. }
  126. }
  127.  
Success #stdin #stdout 0.22s 58940KB
stdin
8
1
45
1
55
1
78
1
65
1
94
2
65
2
94
3
45
3
65
3
11
4
stdout
Enter hash map size: 
Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to insert: Key Inserted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to insert: Key Inserted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to insert: Key Inserted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to insert: Key Inserted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to insert: Key Inserted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to delete: Key Deleted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to delete: Key Deleted

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to search: Key found at index: 5

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to search: Key not found.

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Enter key to search: Key not found.

Choose Operation:
1. Insert
2. Delete
3. Search
4. Exit
Enter choice: Exiting...