Cod sursa(job #1988993)

Utilizator mihailarminia1234Arminia Mihail mihailarminia1234 Data 5 iunie 2017 15:12:34
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.97 kb
#include <bits/stdc++.h>

#define MOD 666013

using namespace std;

struct tip
{
        int info;
        tip *next;
};

tip *HASH[MOD], *p, *nou, *aux;
int N, el, op;

inline void add_element(tip **multime, int x)
{
        for(p = *multime; p != NULL; p = p -> next)
                if(p -> info == x) return;
        nou = (tip*)malloc(sizeof(tip));
        nou -> info = x;
        nou -> next = *multime;
        *multime = nou;
}

inline void delete_element(tip **multime, int x)
{
        if(*multime == NULL) return;
        else if((*multime) -> info == x) (*multime) = (*multime) -> next;
        else
        {
                for(p = *multime; p -> next -> next != NULL; p = p -> next)
                        if(p -> next -> info == x)
                        {
                                aux = p -> next;
                                p -> next = p -> next -> next;
                                free(aux);
                                return;
                        }
                if(p -> next -> info == x)
                {
                        aux = p -> next;
                        p -> next = NULL;
                        free(aux);
                }
        }
}

inline void search_element(tip *multime, int x)
{
        for(p = multime; p != NULL; p = p -> next)
                if(p -> info == x)
                {
                        printf("1\n");
                        return;
                }
        printf("0\n");
}

void Solve(int x, int tip)
{
        if(tip == 1) add_element(&HASH[x % MOD], x);
        else if(tip == 2) delete_element(&HASH[x % MOD], x);
        else search_element(HASH[x % MOD], x);
}

int main()
{
        freopen("hashuri.in", "r", stdin);
        freopen("hashuri.out", "w", stdout);
        scanf("%d", &N);
        for(int i = 1; i <= N; ++i)
        {
                scanf("%d %d", &op, &el);
                Solve(el, op);
        }
        return 0;
}