/*
 * SeperatChaining.java created on 17.02.2005 by M. Schweighauser
 */

import java.util.*;

public class SeperatChaining
{
    private int N; // Tabellengrösse
    private List [] table;
    
    // Default-Konstruktor
    public SeperatChaining()
    {
        this(37);
    }
    
    // Konstruktor mit Angabe der Groesse der Hashtabelle
    public SeperatChaining(int size)
    {
        N = size;
        table = new LinkedList[N];
        
        for (int i=0; i<table.length; i++)
            table[i] = new LinkedList();
    }

    // Suche nach einem String
    public boolean contains(String s)
    {
        int hashValue = s.hashCode() % N; // Passt Hashwert auf Tabelle an
        if (hashValue < 0) hashValue += N;  
        
        return table[hashValue].contains(s);
    }

    // Fuege neuen String ein
    public void insert(String s)
    {
        int hashValue = s.hashCode() % N; // Passt Hashwert auf Tabelle an
        if (hashValue < 0) hashValue += N;  
        
        table[hashValue].add(s);
    }

    // Loesche einen String aus der Hashtabelle
    public void remove(String s)
    {
        int hashValue = s.hashCode() % N; // Passt Hashwert auf Tabelle an
        if (hashValue < 0) hashValue += N;  

        table[hashValue].remove(s);
    }
    
    //  Loescht die Liste
    public void clear()
    {
        for (int i=0; i<table.length; i++)
            table[i].clear();
    }

    public void printHashTable()
    {
        System.out.println("SeperatChainingTable:");
        for (int i=0; i<table.length; i++)
        {
            System.out.print(i + "\t");
            if (table[i].size()==0)
                System.out.println("-");
            else
            {
                System.out.println("");
                Iterator it = table[i].iterator();
                while(it.hasNext())
                    System.out.println("\t" + (String)it.next());
            }
        } // for
    }
}
