Cod sursa(job #2152672)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 5 martie 2018 18:39:54
Problema Hashuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.95 kb
#include <stdio.h>
#include <stdlib.h>

#define mod 199993

int n,x;

typedef struct node{
    int value;
    int freq;
    struct node *next;
}LLIST;

LLIST *hash[mod];

void insert(LLIST **head, int value){
    if((*head) == NULL){
        LLIST *aux = (LLIST *)calloc(1,sizeof(LLIST));
        aux->value = value;
        aux->freq = 1;
        aux->next = NULL;
        (*head) = aux;
    }else{
        LLIST *p = (*head);
        for(; p && p->value != value; p = p->next);

        if(p == NULL){
            LLIST *aux = (LLIST *)calloc(1,sizeof(LLIST));
            aux->value = value;
            aux->freq = 1;
            aux->next = (*head);
            (*head) = aux;
        }
    }
}
void del(LLIST **head, int value){
    if((*head) == NULL){

    }else{
        if((*head)->value == value){
            LLIST *aux = (*head);
            (*head) = (*head)->next;
            free(aux);
            return;
        }

        LLIST *p = (*head), *q;
        for(; p && p->value != value; q = p, p = p->next);

        if(p == NULL){

        }else{
            LLIST *aux = p;
            q->next = p->next;

            free(aux);
        }
    }
}
int get(LLIST *head, int value){
    if(head == NULL){
        return 0;
    }else{
        LLIST *p = head;
        for(; p && p->value != value; p = p->next);
        if(p == NULL){
            return 0;
        }else{
            return 1;
        }
    }
}
int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1; i <= n; ++i){
        int q,x;
        scanf("%d%d",&q,&x);

        int local_hash = x % mod;

        if(q == 1){
            insert(&hash[local_hash],x);
        }else
        if(q == 2){
            del(&hash[local_hash],x);
        }else
        if(q == 3){
            printf("%d\n",get(hash[local_hash],x));
        }
    }

    return 0;
}