Pagini recente » Cod sursa (job #277721) | Cod sursa (job #1147488) | Cod sursa (job #131863) | Cod sursa (job #1711184) | Cod sursa (job #1833839)
#include <fstream>
#include <vector>
#define HASHMAX 666013
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
struct Nod
{
int inf;
Nod* next;
};
typedef struct Nod* Lista;
vector<Lista> Hash;
int func_dispers(int k)
{
return k % HASHMAX;
}
void insert_hash_element(int k)
{
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)
{
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)
{
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;
Hash.resize(HASHMAX);
for (int i = 0; i < n; ++i)
{
int cod, x;
f >> cod >> x;
if (cod == 1)
insert_hash_element(x);
else if (cod == 2)
sterge_hash_element(x);
else if (cod == 3)
g << cautare_hash_element(x) << "\n";
}
f.close();
g.close();
return 0;
}