Lab task Hashing
#include<iostream>
#include<conio.h>
using namespace std;
class node
{
public:
int data;
node* next;
node(int t) : data(t)
{
next=NULL;
}
};
class list
{
private:
node* head;
public:
list() { head=NULL; }
void add(int t)
{
if(head==NULL)
head=new node(t);
else
{
node* temp=head;
head=new node(t);
head->next=temp;
}
}
bool search(int t)
{
for(node *temp=head; temp!=NULL ; temp=temp->next)
if(temp->data==t)
return true;
return false;
}
void print()
{
node* temp;
for(temp=head ; temp!=NULL ; temp=temp->next)
cout<<"->"<<temp->data;
}
};
class Hash
{
private:
list arr[5];
public:
void insert(int t)
{
int loc=((t%10)+((t%100)/10)+((t%1000)/100))%5;
arr[loc].add(t);
}
bool search(int t)
{
int loc=((t%10)+((t%100)/10)+((t%1000)/100))%5;
return arr[loc].search(t);
}
void print()
{
for(int i=0 ; i<5 ; i++)
{
cout<<i;
arr[i].print();
cout<<endl;
}
}
};
void main()
{
Hash table;
cout<<"ENter values and press -1 to end";
int t;
while(true)
{
cin>>t;
if(t==-1)
break;
table.insert(t);
}
cout<<"\nEnter value to be search";
cin>>t;
if(table.search(t))
cout<<"\nValue is in the table";
else
cout<<"\nValue is not in the table";
cout<<"\n\nList is\n";
table.print();
}
#include<conio.h>
using namespace std;
class node
{
public:
int data;
node* next;
node(int t) : data(t)
{
next=NULL;
}
};
class list
{
private:
node* head;
public:
list() { head=NULL; }
void add(int t)
{
if(head==NULL)
head=new node(t);
else
{
node* temp=head;
head=new node(t);
head->next=temp;
}
}
bool search(int t)
{
for(node *temp=head; temp!=NULL ; temp=temp->next)
if(temp->data==t)
return true;
return false;
}
void print()
{
node* temp;
for(temp=head ; temp!=NULL ; temp=temp->next)
cout<<"->"<<temp->data;
}
};
class Hash
{
private:
list arr[5];
public:
void insert(int t)
{
int loc=((t%10)+((t%100)/10)+((t%1000)/100))%5;
arr[loc].add(t);
}
bool search(int t)
{
int loc=((t%10)+((t%100)/10)+((t%1000)/100))%5;
return arr[loc].search(t);
}
void print()
{
for(int i=0 ; i<5 ; i++)
{
cout<<i;
arr[i].print();
cout<<endl;
}
}
};
void main()
{
Hash table;
cout<<"ENter values and press -1 to end";
int t;
while(true)
{
cin>>t;
if(t==-1)
break;
table.insert(t);
}
cout<<"\nEnter value to be search";
cin>>t;
if(table.search(t))
cout<<"\nValue is in the table";
else
cout<<"\nValue is not in the table";
cout<<"\n\nList is\n";
table.print();
}