Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class SLLNode<E> {
- public E element;
- protected SLLNode<E> succ;
- public SLLNode(E element,SLLNode<E> succ)
- {
- this.element = element;
- this.succ = succ;
- }
- E getElement()
- {
- return element;
- }
- public void setElement(E element) {
- this.element = element;
- }
- }
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable // object) and a value (an arbitrary object).
- K key; E value;
- public MapEntry (K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo (K that) {
- // Compare this map entry to that map entry. @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "<" + key + "," + value + ">";
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- private SLLNode<MapEntry<K, E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
- }
- int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, E>> 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, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) {
- // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) 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, E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal // to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ)
- {
- if (key.equals(((MapEntry<K,E>) curr.element).key))
- {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ; return;
- }
- }
- }
- }
- public class RoutingHashJava {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt(); // broj na ruteri shto kje se vnesuvaat
- //System.out.println("test1: n = " + n);
- String interfejs;
- CBHT<String,String> routingTable = new CBHT<String,String>(n);
- String[]zapisi;
- String podatociEdenRuter;
- interfejs = input.nextLine(); // za da go prochita /n od prethodniot input
- for(int i=0;i<n;i++)
- {
- //vlezen interfejs na i-tiot ruter
- interfejs = input.nextLine();
- //ip adresi na mrezhite do koi ruterot ima statichki ruti
- podatociEdenRuter = input.nextLine();
- zapisi = podatociEdenRuter.split(",");
- for(int j=0; j<zapisi.length ; j++)
- {
- routingTable.insert(interfejs, zapisi[j]);
- }
- }
- // vnesuvame broj na obidi za rutiranje
- int brojObidi = input.nextInt();
- //System.out.println("test4: brojObidi " + brojObidi);
- String baranaIP;
- for(int i = 0; i<brojObidi ; i++)
- {
- String keyInterface = input.next(); // ova e eden element od nizata
- baranaIP = input.next();
- SLLNode<MapEntry<String,String>> baranJazol = routingTable.search(keyInterface);
- int daliNajden = 0;
- while(baranJazol!=null)
- {
- String zapis = baranJazol.element.value;
- String[] prviTriTabela = zapis.split("\\.");
- String[] prviTriBarana = baranaIP.split("\\.");
- if((prviTriTabela[0].equals(prviTriBarana[0]))&&(prviTriTabela[1].equals(prviTriBarana[1]))&&(prviTriTabela[2].equals(prviTriBarana[2])))
- {
- System.out.println("postoi");
- daliNajden = 1;
- break;
- }
- baranJazol = baranJazol.succ;
- }
- if(daliNajden ==0)
- System.out.println("ne postoi");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement