Pagini recente » Cod sursa (job #499227) | Cod sursa (job #2659406) | Cod sursa (job #2407132) | Cod sursa (job #2879911) | Cod sursa (job #911342)
Cod sursa(job #911342)
#include<stdio.h>
#include<stdlib.h>
//#include "hashtable.h"
#define MOD 100013
struct list_elem{
int info;
list_elem *prev, *next;
};
class Hash_table{
public:
struct list_elem *pfirst;
void add(int x)
{
struct list_elem *nod;
nod = new struct list_elem;
nod -> info = x;
nod -> prev = NULL;
nod -> next = pfirst;
if(pfirst != NULL)
pfirst -> prev = nod;
pfirst = nod;
}
struct list_elem * find(int x)
{
struct list_elem *nod;
nod = pfirst;
while(nod != NULL)
{
if(nod->info == x)
return nod;
nod = nod->next;
}
return NULL;
}
void remove(int x)
{
struct list_elem *px;
px = find(x);
if(px != NULL)
{
if(px->prev != NULL)
px->prev->next = px->next;
if(px->next != NULL)
px->next->prev = px->prev;
if(px -> prev == NULL)
pfirst = px->next;
delete px;
}
}
Hash_table(){
pfirst = NULL;
}
~Hash_table(){}
};
int main()
{
int x,N,op,i;
FILE *f, *g;
f = fopen("hashuri.in","r");
g = fopen("hashuri.out","w");
fscanf(f,"%d",&N);
Hash_table H[MOD];
for(i=1;i<=N;i++)
{
fscanf(f,"%d %d",&op,&x);
if(op == 1)
{
H[x%MOD].add(x);
continue;
}
if(op == 2)
{
H[x%MOD].remove(x);
continue;
}
fprintf(g,"%d\n", H[x%MOD].find(x) != NULL );
}
fclose(f);
fclose(g);
return 0;
}