Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.Scanner;
- public class HashFun {
- public static void main(String args[]) {
- String text = "24\n" +
- "Mila 19.6.1989\n" +
- "Ivana 19.6.1977\n" +
- "Simon 19.6.1994\n" +
- "Ognen 19.6.1998\n" +
- "Leona 19.6.1999\n" +
- "Martin 19.6.1981\n" +
- "Vesna 13.4.2015\n" +
- "Katerina 12.3.1945\n" +
- "Milan 14.2.1996\n" +
- "Olja 13.2.1988\n" +
- "Bojan 19.6.1988\n" +
- "Bojan 19.7.1978\n" +
- "Bojan 19.7.1988\n" +
- "Bojana 19.6.1988\n" +
- "Bojan 21.6.1988\n" +
- "Bojan 22.7.1988\n" +
- "Bojan 24.7.1988\n" +
- "Bojan 27.7.1988\n" +
- "Ana 19.7.1987\n" +
- "Bojana 19.6.1988\n" +
- "Elena 1.1.1988\n" +
- "Filip 1.12.1988\n" +
- "Filip 2.11.1976\n" +
- "Elena 13.1.1966\n" +
- "6";
- Scanner scanner = new Scanner(text);
- int numOfPeople = Integer.parseInt(scanner.nextLine());
- // Month -> List of Names
- CBHT<Integer, String> map = new CBHT<>(12);
- for (int i = 0; i < numOfPeople; i++) {
- String[] split = scanner.nextLine().split("\\s+");
- String name = split[0];
- Integer month = Integer.parseInt(split[1].split("\\.")[1]);
- map.insertLast(month, name);
- }
- Integer target = Integer.parseInt(scanner.nextLine());
- SLLNode<MapEntry<Integer, String>> current = map.searchList(target);
- System.out.println();
- System.out.println(map);
- if (current == null) {
- System.out.println("Nema");
- return;
- }
- while (current != null) {
- System.out.print(current.element.value + " ");
- current = current.succ;
- }
- // deleteFirst a whole SLL at a certain key
- System.out.println("=====================");
- map.deleteEntry(6, "Mila");
- System.out.println();
- System.out.println(map);
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class MapEntry<K extends Comparable<K>, V> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- V value;
- public MapEntry(K key, V val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo(K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K, V> other = (MapEntry<K, V>) that;
- return this.key.compareTo(other.key);
- }
- public String toString() {
- return "<" + key + "," + value + ">";
- }
- }
- class CBHT<K extends Comparable<K>, V> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K, V>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, V>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, V>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, V>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(curr.element.key)) return curr;
- }
- return null;
- }
- public void insert(K key, V val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, V> newEntry = new MapEntry<K, V>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, V>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, V>) curr.element).key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K, V>>(newEntry, buckets[b]);
- }
- public void insertFirst(K key, V val) {
- MapEntry<K, V> entry = new MapEntry<>(key, val);
- int b = hash(key);
- /**
- * Prepending an element in the SLL */
- SLLNode<MapEntry<K, V>> insert = new SLLNode<>(entry, buckets[b]);
- buckets[b] = insert;
- }
- public void insertLast(K key, V val) {
- MapEntry<K, V> entry = new MapEntry<>(key, val);
- int b = hash(key);
- /**
- * Append an element in the SLL */
- if (buckets[b] != null) {
- SLLNode<MapEntry<K, V>> insert = new SLLNode<>(entry, null);
- SLLNode<MapEntry<K, V>> current = buckets[b];
- if (current.element.value.equals(val)) return;
- while (current.succ != null) {
- if (current.element.value.equals(val)) return;
- current = current.succ;
- }
- if (current.element.value.equals(val)) return;
- current.succ = insert;
- } else {
- insertFirst(key, val);
- }
- }
- public SLLNode<MapEntry<K, V>> searchList(K targetKey) {
- int b = hash(targetKey);
- return buckets[b];
- }
- public void deleteFirst(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K, V>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(curr.element.key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public void deleteLast(K key) {
- int b = hash(key);
- SLLNode<MapEntry<K, V>> current = buckets[b];
- SLLNode<MapEntry<K, V>> successor = buckets[b].succ;
- while (successor.succ != null) {
- current = current.succ;
- successor = successor.succ;
- }
- current.succ = null;
- }
- public void deleteSLL(K key) {
- int b = hash(key);
- buckets[b] = null;
- }
- public void deleteEntry(K key, V val) {
- MapEntry<K, V> entry = new MapEntry<>(key, val);
- int b = hash(key);
- SLLNode<MapEntry<K, V>> previous = buckets[b];
- SLLNode<MapEntry<K, V>> current = buckets[b].succ;
- SLLNode<MapEntry<K, V>> successor = buckets[b].succ.succ;
- if (previous.element.value.equals(val)) {
- deleteFirst(key);
- return;
- }
- while (current.succ != null) {
- if (current.element.value.equals(val)) {
- previous.succ = current.succ;
- }
- previous = previous.succ;
- current = current.succ;
- successor = successor.succ;
- }
- if (current.element.value.equals(val)){
- previous.succ = null;
- }
- }
- public String toString() {
- StringBuilder temp = new StringBuilder();
- for (int i = 0; i < buckets.length; i++) {
- temp.append(i).append(":");
- for (SLLNode<MapEntry<K, V>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp.append(curr.element.toString()).append(" ");
- }
- temp.append("\n");
- }
- return temp.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment