hash表,用链表来解决冲突问题
/* 哈希表 链接法解决冲突问题 */ #include <iostream> using namespace std; struct Node { int nValue; Node *pNext; }; class HashTable { public: HashTable(int n); ~HashTable(); void Insert(int nValue); void InsertList(Node *pHead, int nValue); void Delete(int nValue); void DeleteList(Node *pHead, int nValue); int Search(int nValue); int HashFun(int nValue); void Print(); void DestroyList(Node *pHead); private: Node **pArr; //指针数组 int nSize; }; HashTable::HashTable(int n) { nSize = n; pArr = new Node*[nSize]; for (int i = 0; i < nSize; i++) { pArr[i] = new Node; pArr[i]->pNext = NULL; } } void HashTable::DestroyList(Node *pHead) { Node *pDel = NULL; while(pHead->pNext) { pDel = pHead->pNext; pHead->pNext = pDel->pNext; delete pDel; } delete pHead; pHead = NULL; } HashTable::~HashTable() { for (int i = 0; i < nSize; i++) { DestroyList(pArr[i]); } pArr = NULL; } int HashTable::HashFun(int nValue) { return nValue % 10; } void HashTable::InsertList(Node *pHead, int nValue) { Node *pCur = pHead; Node *pTmp = new Node; pTmp->nValue = nValue; pTmp->pNext = NULL; while(pCur->pNext) { if (nValue < pCur->pNext->nValue) { pTmp->pNext = pCur->pNext; pCur->pNext = pTmp; return; } else if (nValue == pCur->pNext->nValue) { return; } else { pCur = pCur->pNext; } } pCur->pNext = pTmp; } void HashTable::Insert(int nValue) { int nPos = HashFun(nValue); Node *pTmp = new Node; pTmp->nValue = nValue; pTmp->pNext = NULL; if (pArr[nPos] == NULL) { pArr[nPos]->pNext = pTmp; } else { InsertList(pArr[nPos], nValue); } } void HashTable::DeleteList(Node *pHead, int nValue) { Node *pCur = pHead; while(pCur->pNext) { if (pCur->pNext->nValue == nValue) { Node *pDel = pCur->pNext; pCur->pNext = pDel->pNext; delete pDel; return; } else { pCur = pCur->pNext; } } } void HashTable::Delete(int nValue) { if (-1 == Search(nValue)) { cout<<"哈希表中没有该数据!"<<endl; return; } int nPos = HashFun(nValue); DeleteList(pArr[nPos], nValue); } //若无该数据则返回-1 int HashTable::Search(int nValue) { int nPos = HashFun(nValue); if (pArr[nPos]->pNext == NULL) { return -1; } else { Node *pCur = pArr[nPos]->pNext; while (pCur) { if (pCur->nValue == nValue) { return pCur->nValue; } else { pCur = pCur->pNext; } } return -1; } } void HashTable::Print() { for (int i = 0; i < nSize; i++) { Node *pCur = pArr[i]->pNext; while(pCur) { cout<<pCur->nValue<<" "; pCur = pCur->pNext; } cout<<endl; } } int main() { int nArr[] = {1,23,4,6,7,8,0,23,12,4556,34,2,23,5456,783,23,12,556,784,24,12}; int nLen = sizeof(nArr) / sizeof(int); HashTable hTable(10); for (int i = 0; i < nLen; i++) { hTable.Insert(nArr[i]); } cout<<hTable.Search(122)<<endl; hTable.Delete(4); hTable.Print(); system("pause"); return 0; }
代码下载地址:http://download.csdn.net/detail/yincheng01/6858815
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行