#include <iostream>
#include <cstring>
#include <vector>
#include "list.cpp"
#include <cmath>
using namespace std;
struct HashEntry{
   int key;
   List<string> list;
   HashEntry(int k)
   {        
         key=k;
   } 
}; 
class Hash{
  private:
         HashEntry *Table[100];
         int a;
  public:
         Hash(int A);
         void insert(string word);
         void Lookup(string word);
};    
Hash::Hash(int A)
{
    a=A;
}         
void Hash::insert(string word)
{
   int c=0;
   for (int i=0;i<word.size();i++)
   {                                 
       int b=(int)((a^i)*(word[i]));
       c+=b;
   }
   c%=100;
   List<string> list;
   if (Table[c-1]==NULL)     //if the respective bucket doesnot have any string
   Table[c-1]=new HashEntry(c-1);
   Table[c-1]->list.insertAtTail(word);
}                  
void Hash::Lookup(string word)
{
   int c=0;
   for (int i=0;i<word.size();i++)
   {                                 
     int b=(int)((a^i)*(word[i]));
     c+=b;
   }
   cout<<"one"<<endl;
   c%=100;
   Table[c-1]->list.searchFor(word);
   cout<<"two"<<endl;
}
我正在使用单独的链接制作一个哈希表。我的哈希函数正在使用一个常数“a”制作一个多项式方程,其功率随着单词中字母的索引而增加。(a^0xb+a^1xb+a^ 2xb+...) ,其中 b 是正在散列的单词中的一个字母,然后我取 mod(100) 的最终答案。我面临的问题是查找函数。当我测试查找函数时,searchFor () 作为部分链接列表类的一部分的函数不起作用,尽管它本身可以正常工作,并且在我用来调试的 cout<<"one" 之后出现分段错误。我很抱歉打扰,但我只是无法理解这里的问题。链表类文件在下面。我只是粘贴我遇到问题的函数
#ifndef __LIST_H
#define __LIST_H
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
/* This class just holds a single data item. */
template <class T>
struct ListItem
{
   vector<string> words;
   T value;
   ListItem<T> *next;
   ListItem<T> *prev;
   ListItem(T theVal)
   {
       this->value = theVal;
       this->next = NULL;
       this->prev = NULL;
   }
};
/* This is the generic List class */
template <class T>
class List
 {
 ListItem<T> *head;
 public:
  // Constructor
  List();
  // Copy Constructor
  List(const List<T>& otherList);
  // Destructor
  ~List();
  // Insertion Functions
  void insertAtHead(T item);
  void insertAtTail(T item);
  void insertAfter(T toInsert, T afterWhat);
  void insertSorted(T item);
  void printList();
  // Lookup Functions
  ListItem<T> *getHead();
  ListItem<T> *getTail();
  void *searchFor(T item);
  // Deletion Functions
  void deleteElement(T item);
  void deleteHead();
  void deleteTail();
  // Utility Functions
  int length();
};
#endif
template <class T>
void List<T>::searchFor(T item)
{    
 ListItem<T> *temp=head;
 if (temp!=NULL)
 {
    while (temp->next!=NULL)
    {
         T sample=temp->value;
         if (sample==item)
         {      
             cout<<"String found";   
             return;
         }
         temp=temp->next;
    }
    T s=temp->value;
    if (s==item)
    {
       cout<<"String found";        
       return;
    }
  }                   
}