Cod sursa(job #871595)

Utilizator dantheroDan Terhesiu danthero Data 4 februarie 2013 22:05:54
Problema Combinari Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.95 kb
#include <stdio.h>
#include <stdlib.h>

typedef struct nodes
{
    int info;
    struct nodes *next;
} node;

#define MAX 300000
node *lst[MAX];
int n;

int add(int nr)
{
    int ind = nr % MAX;
    node *elm, *crt;

    crt = lst[ind];
    while (crt != NULL && crt->info != nr)
        crt = crt->next;
    if (crt == NULL)
    {
        elm = (node *) malloc(sizeof(node));
        if (!elm)
        {
            return -1;
        }
        elm->info = nr;
        elm->next = lst[ind];
        lst[ind] = elm;
        return 1;
    }
    return 0;
}
int del(int nr)
{
    int ind = nr % MAX;
    node *crt, *elm;
    crt = lst[ind];
    if (crt != NULL)
    {
        if (crt->info == nr)
        {
            lst[ind] = crt->next;
            free(crt);
        }
        else
        {
            while (crt->next != NULL && crt->next->info != nr)
                crt = crt->next;
            if (crt->next != NULL && crt->next->info == nr)
            {
                elm = crt->next;
                crt->next = elm->next;
                free(elm);
                return 1;
            }
        }
    }
    return 0;
}
int sch(int nr)
{
    int ind = nr % MAX;
    node *crt = lst[ind];
    while (crt != NULL && crt->info != nr)
        crt = crt->next;
    if (crt == NULL)
        return 0;
    return 1;
}
int main()
{
    FILE *fin, *fout;
    int op, i, nr;
    fin = fopen("hashuri.in", "r");
    fout = fopen("hashuri.out", "w");
    if (!fin || !fout)
    {
        return 1;
    }
    fscanf(fin, "%d", &n);
    for (i=0; i<n; i++)
    {
        fscanf(fin, "%d %d", &op, &nr);
        switch (op)
        {
        case 1:
            add(nr);
            break;
        case 2:
            del(nr);
            break;
        case 3:
            fprintf(fout, "%d\n", sch(nr));
            break;
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}