Inserisce lelemento v nella tabella se la tabella
/********************************************************** *Inserisce l'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presente * **********************************************************/ public void insert(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1) % dim != key)) j = (j+1) % dim; if(table[j] == -1) { table[j] = v; System. out. println("Elemento "+v+" inserito in posizione "+j); } else if(table[j] == v) System. out. println("Elemeno già inserito"); else System. out. println("Impossibile inserire l'elemento: tabella piena"); }
public class Simple. Hash. Test { public static void main(String[] argv) { Simple. Hash hash = new Simple. Hash(5); System. out. println(hash. to. String()); hash. initialize(); System. out. println(hash. to. String()); hash. insert(61); System. out. println(hash. to. String()); hash. insert(15); System. out. println(hash. to. String()); hash. insert(4); System. out. println(hash. to. String()); hash. insert(24); System. out. println(hash. to. String()); hash. insert(0); System. out. println("Elemento 1 in posizione: "+hash. search(1)); System. out. println("Elemento 33 cancellato : " +hash. cancel(33)); System. out. println("Elemento 61 cancellato: "+ hash. cancel(61)); System. out. println(hash. to. String()); System. out. println("Elemento 24 in posizione" + hash. search(24)); hash. insert(24); System. out. println(hash. to. String()); } }
- Slides: 18