Pagini recente » Cod sursa (job #1855741) | Cod sursa (job #1112026) | Cod sursa (job #1779609) | Cod sursa (job #2182908) | Cod sursa (job #714300)
Cod sursa(job #714300)
#include <fstream>
using namespace std;
struct element
{
int info;
element *next;
};
class hash_table
{
element *q;
int siz;
public:
hash_table();
void insert(int);
void erase(int);
bool empty();
bool find(int);
int size();
element* first();
element* last();
};
hash_table::hash_table()
{
q = NULL;
siz = 0;
}
bool hash_table::empty()
{
if (siz)
return false;
return true;
}
int hash_table::size()
{
return siz;
}
element* hash_table::first()
{
return q;
}
element* hash_table::last()
{
element *p;
p=q;
while (p->next != NULL)
p=p->next;
return p;
}
bool hash_table::find(int x)
{
element *p;
if (empty())
return false;
p = first();
while (p != NULL)
{
if (p->info == x)
return true;
p = p->next;
}
return false;
}
void hash_table::insert(int x)
{
element *p,*newnode;
if (find(x))
return;
if (empty())
{
q = new element;
q->info = x;
q->next = NULL;
}
else
{
p = last();
newnode = new element;
newnode->info = x;
newnode->next = NULL;
p->next = newnode;
}
siz++;
}
void hash_table::erase(int x)
{
element *p, *u;
if (!find(x))
{
if (siz == 1)
{
q = NULL;
}
else
{
u = first();
p = p->next;
while (p != NULL)
{
if (p->info == x)
{
u->next = p->next;
delete p;
break;
}
u = p;
p = p->next;
}
}
siz--;
}
}
class hash
{
hash_table *tables;
int P;
public:
hash();
void insert(int);
void erase(int);
short int find(int);
};
hash::hash()
{
P = 666013;
tables = new hash_table[P];
}
void hash::insert(int x)
{
int table = x%P;
tables[table].insert(x);
}
void hash::erase(int x)
{
int table = x%P;
tables[table].erase(x);
}
short int hash::find(int x)
{
int table = x%P;
if (tables[table].find(x))
return 1;
return 0;
}
int main()
{
int n,op,x;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
hash H;
f>>n;
for (int i=0; i<n; ++i)
{
f>>op>>x;
if (op==1)
H.insert(x);
if (op==2)
H.erase(x);
if (op==3)
g<<H.find(x)<<'\n';
}
return 0;
}