Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.7 kB
0
Indexable
Never
//============================================================================
// Name        : Assignment4.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#define size 97
using namespace std;
class HashEntry
{
	long int pNum;
	string name;

public:
	HashEntry()
	{
		pNum=0;
		name=" ";
	}

	HashEntry(long int pNum,string name)
	{
		this->pNum=pNum;
		this->name=name;
	}
friend class HashTable;
};

class HashTable
{
	HashEntry hashtable[size];
public:
	int HashFnc(long int pNum)
	{
		int hi= pNum%10;
		hi=hi%size;
		if(hi<=2)
		{
			hi=hi*4;
		}
		else if(2<hi<7)
		{
			hi=hi*6;
		}
		else if(6<hi<10)
		{
			hi=hi*9;
		}

		return hi;

	}
	void create(long int num,string name)
	{
		int hi;
		hi=HashFnc(num);
		insert(num,name,hi);

	}
	void insert(long int num,string name,int hi)
	{
		if(hashtable[hi].pNum==0)
		{
			hashtable[hi].pNum=num;
			hashtable[hi].name=name;
		}
		else
		{
			insert(num,name,hi+1);
		}
		return;
	}
	void display()
	{
		for(int i=0;i<size;i++)
		{
			if(hashtable[i].pNum!=0)
			{
				cout<<"Number :"<<hashtable[i].pNum<<endl;
				cout<<"Name :"<<hashtable[i].name<<endl;
			}
		}
	}
	int  search(long int num)
	{
		int hi=HashFnc(num);
		for (int i=0;i<size;i++)
		{
		if(hashtable[i].pNum!=0)
		{
			if(hashtable[i].pNum==num)
			{
				return i;
			}
		}
		}
		return -1;
	}
};
int main() {
	HashTable obj;
	int search;
	long int a=8007946267;
	long int x=8190000007;
	string b="Tanmay";
	obj.create(a,b);
	obj.create(x,"Hello");
	obj.display();
	search=obj.search(x);
	cout<<"Found at index"<<search;
	return 0;
}