Cod sursa(job #561734)

Utilizator sunt_emoSunt emo sunt_emo Data 21 martie 2011 14:33:14
Problema Hashuri Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 1.51 kb
#include <stdio.h>
#include <stdlib.h>
#define P 666013

typedef struct nod
{
    int data;
    struct nod * link;
} nod;

void add (nod **h,int x)
{
    nod *p=h[x%P];
    while (p->link)
    {
        if (p->link->data<=x) p=p->link;
        else break;
    }
    nod *q=(nod*) malloc (sizeof (nod));
    q->data=x;
    q->link=p->link;
    p->link=q;
}

void del (nod **h,int x)
{
    nod *p=h[x%P];
    while (p->link)
    {
        if (p->link->data!=x) p=p->link;
        else break;
    }
    if (p->link)
    {
        nod *q=p->link;
        p->link=q->link;
        free (q);
    }
}

int get (nod **h,int x)
{
    nod *p=h[x%P];
    while (p->link)
    {
        if (p->link->data!=x) p=p->link;
        else break;
    }
    if (p->link) return 1;
    return 0;
}

int main ()
{
    FILE *in,*out;
    int n,op,x,i;
    nod **h=(nod**) malloc (P*sizeof (nod*));
    for (i=0; i<P; i++)
    {
        h[i]=(nod*) malloc (sizeof (nod*));
        h[i]->link=0;
    }
    in=fopen ("hashuri.in","r");
    out=fopen ("hashuri.out","w");
    fscanf (in,"%d",&n);
    for (i=0; i<n; i++)
    {
        fscanf (in,"%d%d",&op,&x);
        switch (op)
        {
            case 1:
                add (h,x);
                break;
            case 2:
                del (h,x);
                break;
            case 3:
                fprintf (out,"%d\n",get (h,x));
                break;
        }
    }
    fclose (in); fclose (out);
    return 0;
}