Cod sursa(job #780359)

Utilizator oana_popfmi - pop oana oana_pop Data 20 august 2012 13:40:44
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
const int m=666013;
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");

int n,i,c;
long e;

struct hash{
       long x;
       hash* next;};
hash* h[666013];

void add(long a)
{
     hash* q=new hash;
     int modul;
     q->x=a;
     modul=e%m;
     q->next=h[modul];
     h[modul]=q;   
}

int search(long a)
{
    int modul;
    modul=a%m;
    hash* q=h[modul];
    while(q)
    {
            if (q->x==a) return 1;
            q=q->next;
    } 
    return 0;
}

void del(int a)
{
     int modul;
     modul=a%m;
     hash* q=h[modul];
     if(q)
     {
     if(q->x==a)
     h[modul]=q->next;
     else
     {
     hash* p;
     p=q;
     q=q->next;
     while(q)
     {
      if(q->x==a)
          p->next=q->next;
          q=q->next;}     
     }      
 }
}

int main()
{
    f>>n;
    for(i=1; i<=n; i++)
    {
             f>>c>>e;
             if(c==1) add(e);
             if(c==2) del(e);
             if(c==3) g<<search(e)<<" ";
             }
}