[java] APS_lab7.4
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- 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 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 CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- 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];
- }
- 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,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 String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- public class RoutingHashJava {
- public static void main (String[] args) throws IOException {
- CBHT<String,String[]> tabela;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- tabela=new CBHT<String,String[]>(2*N);
- for(int i=1;i<=N;i++){
- String interfejs=br.readLine();
- String routing_table=br.readLine();
- String[] ruti = routing_table.split(",");
- tabela.insert(interfejs, ruti);
- }
- int M = Integer.parseInt(br.readLine());
- for (int i=1;i<=M;i++)
- {
- String ip_adresa_interfejs=br.readLine();
- String mreza=br.readLine();
- SLLNode<MapEntry<String,String[]>> teme=tabela.search(ip_adresa_interfejs);
- if(teme!=null)
- {
- String [] rez=teme.element.value;
- String[] bajt_mreza=mreza.split("\\.");
- boolean postoi =false;
- for(int j=0;j<rez.length;j++)
- {
- String[] bajt_tabela=rez[j].split("\\.");
- 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)
- {
- System.out.println("postoi");
- postoi=true;
- break;
- }
- }
- if(!postoi)
- System.out.println("ne postoi");
- }
- else
- System.out.println("ne postoi");
- }
- }
- }
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:
Latest Code Pastes