I am writing a hash table using given interfaces, but I get the error says as the title at
return (IEnumerator<Key>)items.GetEnumerator();
and
foreach (String first in ht);
The entire code shown as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RIT_CS
{
public class NonExistentKey<Key> : Exception
{
public Key BadKey { get; private set; }
public NonExistentKey(Key k) :
base("Non existent key in HashTable: " + k)
{
BadKey = k;
}
}
interface Table<Key, Value> : IEnumerable<Key>
{
void Put(Key k, Value v);
bool Contains(Key k);
Value Get(Key k);
}
public struct KeyValue<Key, Value>
{
public Key K { get; set;}
public Value V { get; set; }
}
class MyTable<Key, Value>:Table<Key, Value>
{
private int size;
private int count=0;
private LinkedList<KeyValue<Key, Value>>[] items;
public MyTable(int size)
{
...
}
public void SetSize(int size)
{
...
}
public LinkedList<KeyValue<Key, Value>>[] GetItems()
{
return items;
}
protected int Getposition(Key k)
{
...
}
protected LinkedList<KeyValue<Key, Value>> GetLinkedList(int position)
{
return items[position];
}
public void Put(Key k, Value v)
{
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator)GetEnumerator();
}
public IEnumerator<Key> GetEnumerator()
{
return (IEnumerator<Key>)items.GetEnumerator();
}
public bool Contains(Key k)
{
}
public Value Get(Key k)
{
}
}
class TableFactory
{
public static MyTable<Key, Value> Make<Key, Value>(int size = 100, double loadThreshold = 0.75)
{
MyTable<Key, Value> mytable = new MyTable<Key, Value>(size);
return mytable;
}
}
class MainClass
{
public static void Main(string[] args)
{
MyTable<String, String> ht = TableFactory.Make<String, String>(10, 0.75);
ht.Put("Joe", "Doe");
ht.Put("Jane", "Brain");
ht.Put("Chris", "Swiss");
try
{
foreach (String first in ht)
{
Console.WriteLine(first + " -> " + ht.Get(first));
}
Console.WriteLine("=========================");
ht.Put("Wavy", "Gravy");
ht.Put("Chris", "Bliss");
foreach (String first in ht)
{
Console.WriteLine(first + " -> " + ht.Get(first));
}
Console.WriteLine("=========================");
Console.Write("Jane -> ");
Console.WriteLine(ht.Get("Jane"));
Console.Write("John -> ");
Console.WriteLine(ht.Get("John"));
}
catch (NonExistentKey<String> nek)
{
Console.WriteLine(nek.Message);
Console.WriteLine(nek.StackTrace);
}
Console.ReadLine();
}
}
}
To look better, I deleted some irrelevant codes.
Just use the first option from https://stackoverflow.com/a/1272677/3162415 and write:
return ((IEnumerable<Key>)items).GetEnumerator();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With