C++実装データ構造ハッシュテーブル

2667 ワード

#include
#include
#include 
using namespace std;
#define HMax 8

typedef struct node
{
	double key;
	string name;
	string address;
	node *next;
}*list;

list HST[HMax];

int Hash(double k)//      
{
	int sum = 0;
	int m;
	while(k>0)
	{
		sum=sum + int(k) % 10;
		k=k/10;
	}

	m = sum % 7;
	return m;
}

void HashinsertChain(list HST[],double key,string name,string address)//       
{
	node *p,*s;
	int n;
	n = Hash(key);
	if(HST[n] == NULL)
	{
		p = new node;
		p->key = key;
		p->name = name;
		p->address = address;
		p->next = NULL;
		HST[n] = p;
	}
	else
	{
		s = HST[n];
		while(s->next != NULL && s->key != key)
			s = s->next;
		if(s->key == key)
		{
			cout<name <key = key;
			p->name = name;
			p->address = address;
			p->next = NULL;
			s->next = p;
		}
	}
}

void HashsearchChain(list HST[],double key)   //      key
{
	int n;
	node *q;
	n = Hash(key);
	q = HST[n];
	while(q!=NULL && q->key != key)
		q = q->next;
	if(q == NULL)
	cout<name<key;
		cout<address<next != NULL && s->key != key)
		{
			p=s;
			s=s->next;
		}
		if(s->key==key)
		{
			cout << fixed << setprecision(0);
			cout<name <key;
			cout<address<>a;
			if(a=="Y")
			{
				if(p==NULL)
					HST[n]=s->next;
				else
					p->next=s->next;
				delete s;
				cout<> name >> key >> address;
	HashinsertChain(HST,key,name,address);
}
void HashshowChain(list HST[])
{
	for(int i=0;iname<key;
			cout<address<next;
		}
	}
}
void view()
{
	cout << "----------------------------------------"<>a;
	if (a != 1 && a != 2 && a != 3 && a!=4)
	{
		cout <> a;
	}
	switch(a)
	{
	case 1:
		HashcreateChain(HST);
		break;
	case 2:
		double snum;
		cout << "          :";
		cin >>snum;
		HashsearchChain(HST,snum);
		break;
	case 3: 
		double  dnum;
		cout << "          :";
		cin >> dnum;
		HashdeleteChain(HST,dnum);
		break;
	case 4:
		HashshowChain(HST);
	}
	cout<>c;
	if(c == 0)
		view();
}
void main()
{
	cout << "                              "<