Pagini recente » Cod sursa (job #1565250) | Cod sursa (job #1848546) | Cod sursa (job #3289914) | Istoria paginii warm-up-2019/solutii/shoturi | Cod sursa (job #1833828)
#include <fstream>
#include <vector>
#define HASHMAX 100009
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
struct Nod
{
int inf;
Nod* next;
};
typedef struct Nod* Lista;
int func_dispers(int k)
{
return k % HASHMAX;
}
void insert_hash_element(int k, vector<Lista> &Hash)
{
int t = func_dispers(k);
Lista p = new Nod;
p -> inf = k;
p -> next = Hash[t];
Hash[t] = p;
}
bool cautare_hash_element(int k, vector<Lista> Hash)
{
int t = func_dispers(k);
Lista p = Hash[t];
while (p != NULL && p -> inf != k)
p = p -> next;
if (p != NULL)
return 1;
return 0;
}
void sterge_hash_element(int k, vector<Lista> &Hash)
{
int t = func_dispers(k);
Lista p = Hash[t];
if (p == NULL)
return;
if (p -> inf == k)
{
Hash[t] = p -> next;
delete p;
}
else
{
Lista q = p -> next;
while (q != NULL && q -> inf != k)
{
q = q -> next;
p = p -> next;
}
if (q != NULL)
{
p -> next = q -> next;
delete q;
}
}
}
int main()
{
int n;
f >> n;
vector<Lista> Hash;
Hash.resize(HASHMAX);
for (vector<Lista>::iterator i = Hash.begin(); i <= Hash.end(); ++i)
*i = NULL;
for (int i = 0; i < n; ++i)
{
int cod, x;
f >> cod >> x;
if (cod == 1)
insert_hash_element(x, Hash);
else if (cod == 2)
sterge_hash_element(x, Hash);
else if (cod == 3)
g << cautare_hash_element(x, Hash) << "\n";
}
f.close();
g.close();
return 0;
}