Pagini recente » Cod sursa (job #1288008) | Cod sursa (job #2386940) | Cod sursa (job #376018) | Cod sursa (job #1617494) | Cod sursa (job #1411428)
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("hashuri.in"));
PrintWriter writer = new PrintWriter("hashuri.out");
int operations = Integer.parseInt(scanner.next());
Hash hash = new Hash(operations);
for ( int i=0; i<operations; i++ ) {
int operationId = Integer.parseInt(scanner.next());
int num = Integer.parseInt(scanner.next());
switch (operationId) {
case 1:
hash.add(num);
break;
case 2:
hash.delete(num);
break;
case 3:
writer.println(hash.contains(num));
break;
}
}
scanner.close();
writer.close();
}
}
class Hash {
private LinkedList<Integer>[] hashTable;
private int size;
Hash(int size) {
hashTable = (LinkedList<Integer>[]) Array.newInstance(LinkedList.class, size);
this.size = size;
}
public void add(int num) {
int hashCode = num % size;
if ( hashTable[hashCode] == null ) {
hashTable[hashCode] = new LinkedList<Integer>();
}
hashTable[hashCode].addFirst(num);
}
public void delete(int num) {
int hashCode = num % size;
if ( hashTable[hashCode] != null ) {
Iterator<Integer> i = hashTable[hashCode].iterator();
while (i.hasNext()) {
if ( i.next() == num ) {
i.remove();
break;
}
i.next();
}
}
}
public int contains(int num) {
int hashCode = num % size;
if ( hashTable[hashCode] != null ) {
for ( Integer i : hashTable[hashCode] ) {
if ( i == num ) {
return 1;
}
}
}
return 0;
}
}