#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;
}
我使用单独的链接taking.my散列函数进行哈希表正在使用恒定的多项式方程“一”,其功率与在一个字信的指数增长。 (a^0xb + a^1xb + a^2xb + ...),其中b是正被哈希的单词中的一个字母,然后我将mod(100)作为最终答案。我面临的问题是查找函数。当我测试查找函数时,部分链接列表类中的searchFor()函数不起作用,尽管它自己可以正常工作,并且在我使用了“1”之后出现了分段错误调试。我很抱歉打扰,但我只是无法理解这里的问题。链表的类文件如下。我只是粘贴我哈维的功能NG问题哈希表(搜索功能)
#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;
}
}
}
“* searchFor()函数不起作用,虽然它自己可以很好地工作。*”你能详细说明这是什么意思吗? –
你知道'^'是一个xor操作符吗? – zch
您的时间段之后的空格将使您的问题更具可读性。 – crashmstr