【问题标题】:Wrong result in HashTable ImplementationHashTable 实现中的错误结果
【发布时间】:2011-12-28 14:40:41
【问题描述】:

我编写了下面的代码来实现HashTable by Chains算法,但是当我在main函数中使用它时,结果是错误的:

#define _SIZE 1000

class HashEntry
{
    public: int Tel;
            string Name;

    public: HashEntry(){}
    public: HashEntry(int Tel, string Name)
    {
        this->Tel = Tel;
        this->Name = Name;
    }
};

class Link {
    public:
    HashEntry data;
        Link *next;

    public:
        Link() : next(NULL){ }

};

void Insert (Link *head, HashEntry data)
{
    Link *t= new Link;
    t->data=data;

    if(head==NULL)
    {
        head = new Link;
        head->next = NULL;
        head->data = data;
        return;
    }

    Link *head2= head;
    while(head2->next!=NULL)
    {     
        if( head2->data.Name == data.Name)
            return;
        head2=head2->next;
    }

    head2->next=t;
}

class  HashTable {
    public:
    Link **a;
    public:
    HashTable()
        {
            a = new Link *[_SIZE];
            for (int i=0; i<_SIZE; i++)
                a[i]=NULL;
        }

    public:
        int HashFonction (string key)
        {
            int res=0;
            for ( int i = 0; i < key.length(); i ++ )
            {
                char ch = key.at(i);
                if( i%2==0)
                    res+=(int)ch;
                else 
                    res-=(int)ch;
            }

            return (int)fabsf((float)res) % _SIZE;
        }

        void HashInsert (string key, int val){
            int index= HashFonction(key);
            HashEntry s(val, key);
            Insert(a[index],s);
        }    

        int HashSearch(string key)
        {
            int inx=HashFonction(key);
            Link *head=a[inx];

            if(head==NULL){
                return -1;
            }

            while(head!=NULL){
                if(head->data.Name==key)
                    return head->data.Tel;

                head=head->next;
            }

            return -1;
        }
};

实现这个类后,我编写了以下代码,但搜索的结果是-1:-/

HashTable ht;
ht.HashInsert("Hossein", 849348);
ht.HashInsert("Ali", 94343);
ht.HashInsert("Fatemeh", 940343);

cout << ht.HashSearch("Ali") << endl; // output = -1 :-/

任何人都可以解释什么是错的吗?

感谢您的关注

【问题讨论】:

  • 你应该调试你的代码并尝试找出它到底是哪里失败了。在插入阶段? [如果是 - 在哪里?] 在搜索中? [为什么搜索失败?]
  • 我觉得还挺对的!我不知道它到底在哪里失败:-/
  • 你应该使用调试器[或者如果你的 IDE 没有调试器,则使用一堆 printf] 并首先检查所有插入是否正确完成......如果没有 - 尝试调查 正是它失败了,然后继续寻找,并以同样的方式检查它。
  • 我觉得Link类有问题!你能找到吗? :-/

标签: c++ data-structures hashtable


【解决方案1】:

你的问题是

void Insert (Link * head, HashEntry data)

改成

void Insert (Link *& head, HashEntry data)

基本上指针是按值传递的

你正在尝试做的事情相当于

void Bar( Link* input )
{   
   input = new Link();
}

Link* foo = 0;
Bar( foo );

Bar 返回 foo 后 仍为 0。当您按值传递指针时,您可以修改它指向的内容而不是指针本身。要使上述内容按预期工作,您可以

替代1

通过引用传递指针

void Bar( Link*& input )
{   
   input = new Link();
}

Link* foo = 0;
Bar( foo );

替代方案 2

将指针传递给指针

void Bar( Link** input )
{   
   *input = new Link();
}

Link* foo = 0;
Bar( &foo );

【讨论】:

    【解决方案2】:

    您将指针头按值传递给函数插入,因此类哈希表中的数组“a”不会改变。通过引用传递解决了这个问题:

    void Insert (Link *&head, HashEntry data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2011-10-31
      相关资源
      最近更新 更多