Cod sursa(job #2605973)

Utilizator geanyhalavDumitrahce Geani geanyhalav Data 26 aprilie 2020 16:09:05
Problema Hashuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.86 kb
	
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode{
    int value;
    struct ListNode *next;
}ListNode;
 
typedef struct List{
    struct ListNode *root;
}List;
 
List * createList(){
    List *newList = (List *)malloc(sizeof(List));
    newList->root = NULL;
    return newList;
}
 
void addElem(List *list, int value){
    if(list == NULL)
        return;
    ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
    newNode->value = value;
    if(list->root == NULL){
        list->root = newNode;
        newNode->next = NULL;
    }
    else{
        newNode->next = list->root;
        list->root = newNode;
    }
}
 
void delElem(List *list, int value){
    if(list == NULL || list->root == NULL)
        return;
    if(list->root->next == NULL && list->root->value == value){
        free(list->root);
        list->root = NULL;
    }
    else{
        ListNode *crtNode = list->root;
        ListNode *antNode = NULL;
        while(crtNode != NULL){
            if(crtNode->value == value){
                if(antNode == NULL){
                    list->root = crtNode->next;
                }
                else{
                    antNode->next = crtNode->next;
                }
                free(crtNode);
            }
            antNode = crtNode;
            crtNode = crtNode->next;
        }
    }
}
 
int checkIfExists(List *list, int value){
    if(list == NULL)
        return 0;
    ListNode *crtNode = list->root;
    while(crtNode != NULL){
        if(crtNode->value == value)
            return 1;
        crtNode = crtNode->next;
    }
    return 0;
}
 
void destroyList(List *list){
    if(list == NULL)
        return;
    ListNode *crtNode = list->root;
    while(crtNode != NULL){
        ListNode *aux = crtNode->next;
        free(crtNode);
        crtNode = aux;
    }
    free(list);
}
#define MOD 666013
 
int noTests, value, type;
 
List* HashTable[MOD];
 
void Initialize(){
    for(int i = 0; i < MOD; ++ i)
        HashTable[i] = createList();
}
 
void Destroy(){
    for(int i = 0; i < MOD; ++ i)
        destroyList(HashTable[i]);
}
 

int main(void)
{
    FILE* in = fopen("hashuri.in", "r");
    FILE* out = fopen("hashuri.out", "w");
    fscanf(in, "%d", &noTests);
    Initialize();
    for(int i = 0; i < noTests; i++)
    {
        fscanf(in, "%d %d", &type, &value);
        int valueMod = value %MOD; //aplic functia de hash 
        switch (type)
        {
        case 1:
            if(!checkIfExists(HashTable[valueMod], value))
                addElem(HashTable[valueMod], value);
            break;
        case 2:
            delElem(HashTable[valueMod], value);
            break;
        case 3:
            fprintf(out, "%d\n", checkIfExists(HashTable[valueMod], value));
            break;
        }
    }
    return 0;
}