[java] APS_lab7.4

Viewer

copydownloadembedprintName: APS_lab7.4
  1. import java.io.BufferedReader;
  2.  
  3. import java.io.IOException;
  4.  
  5. import java.io.InputStreamReader;
  6.  
  7.  
  8.  
  9. class MapEntry<extends Comparable<K>,E> implements Comparable<K> {
  10.  
  11.  
  12.  
  13.     // Each MapEntry object is a pair consisting of a key (a Comparable 
  14.  
  15.     // object) and a value (an arbitrary object).
  16.  
  17.     K key;
  18.  
  19.     E value;
  20.  
  21.  
  22.  
  23.     public MapEntry (K key, E val) {
  24.  
  25.         this.key = key;
  26.  
  27.         this.value = val;
  28.  
  29.     }
  30.  
  31.     
  32.  
  33.     public int compareTo (K that) {
  34.  
  35.     // Compare this map entry to that map entry.
  36.  
  37.         @SuppressWarnings("unchecked")
  38.  
  39.                 MapEntry<K,E> other = (MapEntry<K,E>) that;
  40.  
  41.         return this.key.compareTo(other.key);
  42.  
  43.     }
  44.  
  45.  
  46.  
  47.     public String toString () {
  48.  
  49.         return "<" + key + "," + value + ">";
  50.  
  51.     }
  52.  
  53. }
  54.  
  55.  
  56.  
  57. class SLLNode<E> {
  58.  
  59.         protected E element;
  60.  
  61.         protected SLLNode<E> succ;
  62.  
  63.  
  64.  
  65.         public SLLNode(E elem, SLLNode<E> succ) {
  66.  
  67.                 this.element = elem;
  68.  
  69.                 this.succ = succ;
  70.  
  71.         }
  72.  
  73.  
  74.  
  75.         @Override
  76.  
  77.         public String toString() {
  78.  
  79.                 return element.toString();
  80.  
  81.         }
  82.  
  83. }
  84.  
  85.  
  86.  
  87. class CBHT<extends Comparable<K>, E> {
  88.  
  89.  
  90.  
  91.         // An object of class CBHT is a closed-bucket hash table, containing
  92.  
  93.         // entries of class MapEntry.
  94.  
  95.         private SLLNode<MapEntry<K,E>>[] buckets;
  96.  
  97.  
  98.  
  99.         @SuppressWarnings("unchecked")
  100.  
  101.         public CBHT(int m) {
  102.  
  103.                 // Construct an empty CBHT with m buckets.
  104.  
  105.                 buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  106.  
  107.         }
  108.  
  109.  
  110.  
  111.         private int hash(K key) {
  112.  
  113.                 // Translate key to an index of the array buckets.
  114.  
  115.                 return Math.abs(key.hashCode()) % buckets.length;
  116.  
  117.         }
  118.  
  119.  
  120.  
  121.         public SLLNode<MapEntry<K,E>> search(K targetKey) {
  122.  
  123.                 // Find which if any node of this CBHT contains an entry whose key is
  124.  
  125.                 // equal
  126.  
  127.                 // to targetKey. Return a link to that node (or null if there is none).
  128.  
  129.                 int b = hash(targetKey);
  130.  
  131.                 for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  132.  
  133.                         if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  134.  
  135.                                 return curr;
  136.  
  137.                 }
  138.  
  139.                 return null;
  140.  
  141.         }
  142.  
  143.  
  144.  
  145.         public void insert(K key, E val) {                // Insert the entry <key, val> into this CBHT.
  146.  
  147.                 MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  148.  
  149.                 int b = hash(key);
  150.  
  151.                 for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  152.  
  153.                         if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  154.  
  155.                                 // Make newEntry replace the existing entry ...
  156.  
  157.                                 curr.element = newEntry;
  158.  
  159.                                 return;
  160.  
  161.                         }
  162.  
  163.                 }
  164.  
  165.                 // Insert newEntry at the front of the 1WLL in bucket b ...
  166.  
  167.                 buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  168.  
  169.         }
  170.  
  171.  
  172.  
  173.         public void delete(K key) {
  174.  
  175.                 // Delete the entry (if any) whose key is equal to key from this CBHT.
  176.  
  177.                 int b = hash(key);
  178.  
  179.                 for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  180.  
  181.                         if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  182.  
  183.                                 if (pred == null)
  184.  
  185.                                         buckets[b] = curr.succ;
  186.  
  187.                                 else
  188.  
  189.                                         pred.succ = curr.succ;
  190.  
  191.                                 return;
  192.  
  193.                         }
  194.  
  195.                 }
  196.  
  197.         }
  198.  
  199.  
  200.  
  201.         public String toString() {
  202.  
  203.                 String temp = "";
  204.  
  205.                 for (int i = 0; i < buckets.length; i++) {
  206.  
  207.                         temp += i + ":";
  208.  
  209.                         for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  210.  
  211.                                 temp += curr.element.toString() + " ";
  212.  
  213.                         }
  214.  
  215.                         temp += "\n";
  216.  
  217.                 }
  218.  
  219.                 return temp;
  220.  
  221.         }
  222.  
  223.  
  224.  
  225. }
  226.  
  227.  
  228.  
  229. public class RoutingHashJava {
  230.  
  231.          public static void main (String[] args) throws IOException {
  232.  
  233.                 
  234.  
  235.                  CBHT<String,String[]> tabela;
  236.  
  237.                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  238.  
  239.              int N = Integer.parseInt(br.readLine());
  240.  
  241.                     
  242.  
  243.                  
  244.  
  245.                  tabela=new CBHT<String,String[]>(2*N);
  246.  
  247.                
  248.  
  249.               
  250.  
  251.                 
  252.  
  253.                 for(int i=1;i<=N;i++){
  254.  
  255.                         String interfejs=br.readLine();
  256.  
  257.                         String routing_table=br.readLine();
  258.  
  259.                        
  260.  
  261.                                 String[] ruti = routing_table.split(",");
  262.  
  263.                                
  264.  
  265.                                 tabela.insert(interfejs, ruti);
  266.  
  267.                        
  268.  
  269.                         }
  270.  
  271.                 
  272.  
  273.               
  274.  
  275.                 int M = Integer.parseInt(br.readLine());
  276.  
  277.                 for (int i=1;i<=M;i++)
  278.  
  279.                 {
  280.  
  281.                         String ip_adresa_interfejs=br.readLine();
  282.  
  283.                         String mreza=br.readLine();
  284.  
  285.                      
  286.  
  287.                         SLLNode<MapEntry<String,String[]>> teme=tabela.search(ip_adresa_interfejs);
  288.  
  289.                     if(teme!=null)
  290.  
  291.                     {
  292.  
  293.                            
  294.  
  295.                             String [] rez=teme.element.value;
  296.  
  297.                             String[] bajt_mreza=mreza.split("\\.");
  298.  
  299.                            
  300.  
  301.                             boolean postoi =false;
  302.  
  303.                             for(int j=0;j<rez.length;j++)
  304.  
  305.                             {
  306.  
  307.                                     String[] bajt_tabela=rez[j].split("\\.");
  308.  
  309.                                    
  310.  
  311.                                     if(bajt_tabela[0].compareTo(bajt_mreza[0])==0&&bajt_tabela[1].compareTo(bajt_mreza[1])==0&&bajt_tabela[2].compareTo(bajt_mreza[2])==0)
  312.  
  313.                                     {
  314.  
  315.                                             System.out.println("postoi");
  316.  
  317.                                             postoi=true;
  318.  
  319.                                             break;
  320.  
  321.                                     }
  322.  
  323.                                    
  324.  
  325.                                    
  326.  
  327.                             }
  328.  
  329.                             if(!postoi)
  330.  
  331.                             System.out.println("ne postoi");
  332.  
  333.                            
  334.  
  335.                     }
  336.  
  337.                     else
  338.  
  339.                             System.out.println("ne postoi");
  340.  
  341.                        
  342.  
  343.                 }
  344.  
  345.                 
  346.  
  347.                 
  348.  
  349.                
  350.  
  351.          }
  352.  
  353. }

Editor

You can edit this paste and save as new:


File Description
  • APS_lab7.4
  • Paste Code
  • 04 Feb-2023
  • 5.25 Kb
You can Share it: