哈希表(链地址法)

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <vector>
using namespace std;

struct LNode
{
	int data;
	LNode *next;
};


class HashTable
{
private:
	int m;
	LNode **HT;
	int Hash(int key, int m);
public:
	HashTable(int MaxSize);
	~HashTable();
	void FirstPage();
	void CreateTable(int arr[], int n);
	void Insert(int key);
	bool Delete(int key);
    int Search(int key);
	void Print();
};

HashTable::HashTable(int MaxSize)
{
	m = MaxSize;
	HT = new LNode*[m];
	for(int i=0; i<m; ++i)
	{
		HT[i] = NULL;
	}
}

HashTable::~HashTable()
{
	LNode *p;
	for(int i=0; i<m; i++)
	{
		p = HT[i];
		while(p)
		{
			HT[i] = p->next;
			delete p;
			p = HT[i];
		}
	}
	delete []HT;
}

inline int HashTable::Hash(int key, int m)
{
	return key % m;
}

void HashTable::CreateTable(int arr[], int n)
{
	if(n>m)
	{
		cout<<"error"<<endl;
		return ;
	}
	for(int i=0; i<n; ++i)
	{
		Insert(arr[i]);
		cout<<arr[i]<<"  ";
	}
	cout<<endl;
}


void HashTable::Insert(int key)
{
	int add = Hash(key, m);
	LNode *p = new LNode;

	p->data = key;
	p->next = HT[add];
	HT[add] = p;
}


int HashTable::Search(int key)
{
	int add = Hash(key, m);
	LNode *p = HT[add];
	while(p)
	{
		if(p->data == key)
			return add+1;
		else
			p = p->next;
	}
	return 0;
}


bool HashTable::Delete(int key)
{
	int add = Hash(key,m);
	LNode *p = HT[add];
	if(p == NULL)
		return false;
	//删除头结点
	if(p->data == key)
	{
		HT[add] = p->next;
		delete p;
		return true;
	}
	
	//删除非头结点
	LNode *q = p->next;
	while(q != NULL)
	{
		if(q->data == key)
		{
			p->next = q->next;
			delete q;
			return true;
		}
		else
		{
			p = q;
			q = q->next;
		}
	}
	return false;
}


void HashTable::Print()
{
	LNode *p ;
	for(int i=0; i<m; ++i)
	{
		p = HT[i];
		cout<<""<<setw(2)<<"i : "<<i<<"  ";
	    while(p)
		{
		    cout<<setw(2)<<p->data<<"  ";
			p = p->next;
		}
		cout<<endl;
	}
}

void HashTable::FirstPage()
{
	cout<<"★★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl
		<<"※                     功能列表                     ※" <<endl
	    <<"※                                                  ※" <<endl
		<<"※         1---创建散列表     2---插入元素          ※" <<endl
		<<"※         3---删除元素       4---查找元素          ※" <<endl
	    <<"※         5---打印哈希表     6---退出系统          ※" <<endl
		<<"※                                                  ※" <<endl
		<<"★★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl<<endl;
}

void main()
{
	
	int key;
	int m = 11;
	HashTable ht(m);
 
	int arr[10] = {44,15,14,19,10,29,27,9,2,30};

	while(1)
	{
		system("cls");
		ht.FirstPage();
		cout<<"请输入你的选择(1-5):"<<endl;

		switch( getch() )
		{
			case '1':
				cout<<"创建散列表"<<endl;
				ht.CreateTable(arr, 10);
				system("pause");
				break;

			case '2':
				cout<<"输入待插入的数值:"<<endl;
				cin>>key;
				ht.Insert(key);
				system("pause");
				break;
    		
			case '3':
				cout<<"输入待删除的数值:"<<endl;
				cin>>key;
				if( ht.Delete(key) )
					cout<<"删除成功"<<endl;
				else
					cout<<"删除不成功"<<endl;
				system("pause");
				break;
			
			case '4':
				cout<<"输入待查找的数值:"<<endl;				
				cin>>key;
				if( ht.Search(key) )
				{
					cout<<"查找成功"<<endl;
					cout<<"元素在第"<<ht.Search(key)<<"个链表"<<endl;
				}
				else
				{
					cout<<"查找不成功"<<endl;
				}
				
				system("pause");
				break;
			
			case '5':
				cout<<"打印哈希表"<<endl;
				ht.Print();
				system("pause");
				break;

			case '6':
				cout<<"谢谢使用,再见!"<<endl;
				system("pause");
				return;
			
			default:
				cout<<"输入错误,请重新输入"<<endl;
				system("pause");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值