使用给定的表C#在C#中实现LinkedHashTable

时间:2013-09-06 01:18:41

标签: c# interface linked-list hashtable

对于学校作业,我必须在C#中创建一个LinkedHashTable。老师给了我一个必须经过的Table界面,但是我有点迷失我是否在我的LinkedHashTable类中创建一个HashTable / Dictionary,好像它是一个数据成员并且做了什么管理到把它联系起来。 我最初做的是做一个:

Dictionary<Key, List<Value>> hash;

在我创建的LinkedHashTable类中,我实现的get,put和contains方法与该结构有关。这是表格界面:

interface Table<Key, Value> : IEnumerable<Key>
{
    /// <summary>
    /// Add a new entry in the hash table. If an entry with the
    /// given key already exists, it is replaced without error.
    /// put() always succeeds.
    /// (Details left to implementing classes.)
    /// </summary>
    /// <param name="k">the key for the new or existing entry</param>
    /// <param name="v">the (new) value for the key</param>
    void Put(Key k, Value v);

    /// <summary>
    /// Does an entry with the given key exist?
    /// </summary>
    /// <param name="k">the key being sought</param>
    /// <returns>true iff the key exists in the table</returns>
    bool Contains(Key k);

    /// <summary>
    /// Fetch the value associated with the given key.
    /// </summary>
    /// <param name="k">The key to be looked up in the table</param>
    /// <returns>the value associated with the given key</returns>
    /// <exception cref="NonExistentKey">if Contains(key) is false</exception>
    Value Get(Key k);
}

在测试文件中,他有类似的东西:

ht.Put("Chris", "Swiss");
try
{

foreach (String first in ht)
{
     Console.WriteLine("5");
     Console.WriteLine(first + " -> " + ht.Get(first));
}

整个foreach循环让我觉得我应该以一种方式实现我的类,这样它本身就是一个LinkedHashTable,而不仅仅是一个HashTable作为成员的类。不幸的是,我很担心如何做到这一点。任何建议都会很好。

1 个答案:

答案 0 :(得分:0)

也许有些阅读界面可能有助于回答您的问题。您创建的某些类可能需要实现给定的接口。

http://msdn.microsoft.com/en-us/library/87d83y5b.aspx