Advertisement
ivana_andreevska

Zadaca so lekovi

Feb 9th, 2022
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Scanner;
  3.  
  4. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  5.  
  6.     // Each MapEntry object is a pair consisting of a key (a Comparable
  7.     // object) and a value (an arbitrary object).
  8.     K key;
  9.     E value;
  10.  
  11.     public MapEntry (K key, E val) {
  12.         this.key = key;
  13.         this.value = val;
  14.     }
  15.  
  16.     public int compareTo (K that) {
  17.         // Compare this map entry to that map entry.
  18.         @SuppressWarnings("unchecked")
  19.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  20.         return this.key.compareTo(other.key);
  21.     }
  22.  
  23.     public String toString () {
  24.         return "<" + key + "," + value + ">";
  25.     }
  26. }
  27. class CBHT<K extends Comparable<K>, E> {
  28.  
  29.     // An object of class CBHT is a closed-bucket hash table, containing
  30.     // entries of class MapEntry.
  31.     private SLLNode<MapEntry<K,E>>[] buckets;
  32.  
  33.     @SuppressWarnings("unchecked")
  34.     public CBHT(int m) {
  35.         // Construct an empty CBHT with m buckets.
  36.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  37.     }
  38.  
  39.     private int hash(K key) {
  40.         // Translate key to an index of the array buckets.
  41.         return Math.abs(key.hashCode()) % buckets.length;
  42.     }
  43.  
  44.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  45.         // Find which if any node of this CBHT contains an entry whose key is
  46.         // equal
  47.         // to targetKey. Return a link to that node (or null if there is none).
  48.         int b = hash(targetKey);
  49.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  50.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  51.                 return curr;
  52.         }
  53.         return null;
  54.     }
  55.  
  56.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  57.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  58.         int b = hash(key);
  59.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  60.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  61.                 // Make newEntry replace the existing entry ...
  62.                 curr.element = newEntry;
  63.                 return;
  64.             }
  65.         }
  66.         // Insert newEntry at the front of the 1WLL in bucket b ...
  67.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  68.     }
  69.  
  70.     public void delete(K key) {
  71.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  72.         int b = hash(key);
  73.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  74.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  75.                 if (pred == null)
  76.                     buckets[b] = curr.succ;
  77.                 else
  78.                     pred.succ = curr.succ;
  79.                 return;
  80.             }
  81.         }
  82.     }
  83.  
  84.     public String toString() {
  85.         String temp = "";
  86.         for (int i = 0; i < buckets.length; i++) {
  87.             temp += i + ":";
  88.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  89.                 temp += curr.element.toString() + " ";
  90.             }
  91.             temp += "\n";
  92.         }
  93.         return temp;
  94.     }
  95.  
  96. }
  97. class SLLNode<E> {
  98.     protected E element;
  99.     protected SLLNode<E> succ;
  100.  
  101.     public SLLNode(E elem, SLLNode<E> succ) {
  102.         this.element = elem;
  103.         this.succ = succ;
  104.     }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         return element.toString();
  109.     }
  110. }
  111.  
  112. public class Main {
  113.     public static void main(String[] args) {
  114.         Scanner input=new Scanner(System.in);
  115.         int n=input.nextInt();
  116.         CBHT<String,Lek>tabela=new CBHT<>(2*n);
  117.  
  118.         for(int i=0;i<n;i++)
  119.         {
  120.             String ime=input.next();
  121.             String namena=input.next();
  122.             int cena=input.nextInt();
  123.  
  124.             SLLNode<MapEntry<String,Lek>> curr=tabela.search(namena);
  125.  
  126.             if(curr!=null)
  127.             {
  128.                 if(cena <curr.element.value.cena)
  129.                 {
  130.                     tabela.insert(namena,new Lek(ime,cena));
  131.                 }
  132.             }
  133.             else
  134.             {
  135.                 tabela.insert(namena,new Lek(ime,cena));
  136.             }
  137.         }
  138.         String namena=input.next();
  139.         SLLNode<MapEntry<String,Lek>> cur=tabela.search(namena);
  140.         if(cur!=null)
  141.         {
  142.             System.out.println(cur.element.value.ime);
  143.         }
  144.         else System.out.println("Nema lek za baranata namena vo magacin");
  145.     }
  146. }
  147. class Lek
  148. {
  149.     String ime;
  150.     int cena;
  151.  
  152.     public Lek(String ime , int cena)
  153.     {
  154.         this.ime=ime;
  155.         this.cena=cena;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement