#include<iostream>
#include<vector>
#include<list>
#define TR(a,it) for(typeof((a).begin()) it = (a).begin(); it !=
(a).end(); ++it)
using namespace std;
void Insert(int key, int m, vector< list<int> > &v);
list<int>::iterator Search(int key, int m, vector< list<int> > &v);
void Delete(int key, int m, vector< list<int> > &v);
int main()
{
int m,choice,key;
vector< list<int> > v;
cout << "Enter the no of slots" << endl;
cin >> m;
v.resize(m);
while(1)
{
cout << "1. Insert\n2. Search \n3. Delete \n4. Exit\n";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter the key\n";
cin >> key;
Insert(key, m, v);
break;
case 2:
cout << "Enter the key\n";
cin >> key;
Search(key, m, v);
break;
case 3:
cout << "Enter the key\n";
cin >> key;
Delete(key, m, v);
break;
case 4:
exit(0);
}
}
}
int h(int key, int m)
{
return key % m;
}
void Insert(int key, int m, vector< list<int> > &v)
{
int slot = h(key,m);
if(Search(key,m,v) == NULL)
{
cout << "Inserting...\n";
v[slot].push_back(key);
cout << "Insertion Completed!!\n";
}
}
list<int>::iterator Search(int key, int m, vector< list<int> > &v)
{
int slot = h(key, m);
cout << "Searching...";
TR(v[slot], it)
{
if(*it == key)
{
cout << "Key found!!\n";
return it;
}
}
cout << "key not found!!\n";
return NULL; //we can use like this also:
(list<int> iterator::i)NULL
}
void Delete(int key, int m, vector< list<int> > &v)
{
int slot = h(key, m);
list<int>::iterator i;
i = Search(key, m, v);
if(i != NULL)
{
v[slot].erase(i);
cout << "Deletion Completed!!\n";
}
}
This is not a tested code... it may contain bugs!!
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.