-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
executable file
·190 lines (149 loc) · 5.79 KB
/
Copy pathHashTable.java
File metadata and controls
executable file
·190 lines (149 loc) · 5.79 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
/**
*
* @author egehanhatipoglu
*/
public class HashTable<K, V> {
// INNER CLASS: HashNode (Like a LinkedList node)
private class HashNode {
K key;
V value;
HashNode next;
public HashNode(K key, V value) {
this.key = key;
this.value = value;
this.next = null;
}
}
private HashNode[] buckets; // Array of linked lists
private int capacity; // Size of array
private int size; // Number of elements
// Constructor: Calculates the table capacity using the Student ID.
// This ensures that the internal size of the data structure is unique
// to this specific project submission, preventing identical memory footprints.
@SuppressWarnings("unchecked")
public HashTable(int studentId) {
this.capacity = (studentId % 100) + 50;
this.buckets = (HashNode[]) new HashTable.HashNode[capacity];
this.size = 0;
}
// Hash Function: Converts a generic key into a valid array index.
// It uses the modulo operator (%) to ensure the index fits within the 'capacity'.
// Includes a check to convert negative hash codes to positive integers.
private int hash(K key) {
int hashCode = key.hashCode();
int index = hashCode % capacity;
if (index < 0) {
index = index * -1;
}
return index;
}
// PUT: Inserts or updates a key-value pair.
// Implements 'Chaining' for collision resolution: if the calculated bucket index
// is already occupied, the new node is added to the front (head) of the linked list.
public void put(K key, V value) {
int index = hash(key);
HashNode head = buckets[index];
// 1. Update: Check if key exists in the chain and update value
HashNode current = head;
while (current != null) {
if (current.key.equals(key)) {
current.value = value;
return;
}
current = current.next;
}
// 2. Insert: Key not found, add new node to the head of the bucket
HashNode newNode = new HashNode(key, value);
newNode.next = head;
buckets[index] = newNode;
size++;
}
// GET: Retrieves a value based on its key in O(1) average time.
// It computes the hash index and traverses the linked list at that bucket
// to find the matching key.
public V get(K key) {
int index = hash(key);
HashNode current = buckets[index];
while (current != null) {
if (current.key.equals(key)) {
return current.value;
}
current = current.next;
}
return null; // Key not found
}
// REMOVE: Deletes a key-value pair from the table.
// Handles pointer manipulation to safely remove a node from the linked list chain,
// whether it is the head node or a node in the middle/end.
public V remove(K key) {
int index = hash(key);
HashNode current = buckets[index];
HashNode prev = null;
while (current != null) {
if (current.key.equals(key)) {
// Found the node to remove
if (prev == null) {
// Case 1: Removing the first node in the bucket
buckets[index] = current.next;
} else {
// Case 2: Removing a node from the middle or end
prev.next = current.next;
}
size--;
return current.value;
}
prev = current;
current = current.next;
}
return null;
}
// ... (Remaining methods: containsKey, size, isEmpty, values, display)
// These are standard utility methods and do not require complex documentation.
public boolean containsKey( K key) {
return get(key) != null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public java.util.ArrayList<V> values() {
java.util.ArrayList<V> result = new java.util.ArrayList<>();
for (int i = 0; i < capacity; i++) {
HashNode current = buckets[i];
while (current != null) {
result.add(current.value);
current = current.next;
}
}
return result;
}
public void display() {
System.out.println("\n=== Hash Table ===");
System.out.println("Size: " + size + ", Capacity: " + capacity);
int usedBuckets = 0;
for (int i = 0; i < capacity; i++) {
if (buckets[i] != null) {
usedBuckets++;
System.out.print("Bucket " + i + ": ");
HashNode current = buckets[i];
while (current != null) {
System.out.print("[" + current.key + "=" + current.value + "] -> ");
current = current.next;
}
System.out.println("null");
}
}
System.out.println("Used Buckets: " + usedBuckets + "/" + capacity);
}
}