示例#1
0
    public void Put(int key, int value)
    {
        int hash = (key % TABLE_SIZE);

        if (table[hash] == null)
        {
            table[hash] = new LinkedHashEntry(key, value);
        }
        else
        {
            LinkedHashEntry entry = table[hash];
            while (entry.GetNext() != null && entry.GetKey() != key)
            {
                entry = entry.GetNext();
            }
            if (entry.GetKey() == key)
            {
                entry.SetValue(value);
            }
            else
            {
                entry.SetNext(new LinkedHashEntry(key, value));
            }
        }
    }
示例#2
0
    public void Remove(int key)
    {
        int hash = (key % TABLE_SIZE);

        if (table[hash] != null)
        {
            LinkedHashEntry prevEntry = null;
            LinkedHashEntry entry     = table[hash];

            while (entry.GetNext() != null && entry.GetKey() != key)
            {
                prevEntry = entry;
                entry     = entry.GetNext();
            }

            if (entry.GetKey() == key)
            {
                if (prevEntry == null)
                {
                    table[hash] = entry.GetNext();
                }
                else
                {
                    prevEntry.SetNext(entry.GetNext());
                }
            }
        }
    }
示例#3
0
    public int Get(int key)
    {
        int hash = (key % TABLE_SIZE);

        if (table[hash] == null)
        {
            return(-1);
        }
        else
        {
            LinkedHashEntry entry = table[hash];
            while (entry != null && entry.GetKey() != key)
            {
                entry = entry.GetNext();
            }
            if (entry == null)
            {
                return(-1);
            }
            else
            {
                return(entry.GetValue());
            }
        }
    }