Cod sursa(job #1306953)

Utilizator obidanDan Ganea obidan Data 31 decembrie 2014 18:37:36
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;

const int M = 666013;

typedef struct _List{
    int val;
    _List* Next;
}List;

typedef List* Hash[M];

Hash H;

void init()
{
    for(int i=0;i < M; i++)
    {
        H[i]=NULL;
    }
}
int hash_func(int value)
{
    return value % M ;
}

void Delete(int value)
{
    int where = hash_func(value);
    List *p;
    p = H[where];
    if((p != NULL) && p->val==value)
    {
        H[where]=H[where]->Next;
        return ;
    }
    for(; p!=NULL && p->Next !=NULL; p = p->Next)
    {
        if(((p->Next)->val) == value)
        {
            p->Next=p->Next->Next;
            return;
        }

    }
}

void Add(int value)
{
    int where = hash_func(value);
    List *p;
    p = (List *)malloc(sizeof(List));
    p->val = value;
    p->Next = H[where];
    H[where] = p;

}



int Query(int value)
{
    int where = hash_func(value);
    List* p;
    for(p = H[where]; p!=NULL ; p = p->Next)
    {
        if((p->val) == value) return 1;
    }
    return 0;
}

int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    int i,j,n,x,y;
    init();
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d %d",&x,&y);
        if(x==1)
        {
            Add(y);
        }
        else if(x==2)
        {
            Delete(y);
        }
        else
        {
            printf("%d\n",Query(y));
        }
    }

}