Cod sursa(job #1366121)

Utilizator RazzinnatorRazvan Brinzea Razzinnator Data 28 februarie 2015 19:54:00
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.16 kb
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

#define HASH_SIZE 11113

FILE *f = fopen( "hashuri.in", "r" );
FILE *g = fopen( "hashuri.out", "w" );

typedef struct List
{
    long long value;
    List *next;
} List;

void print_list( List *cap )
{
    while( cap != NULL )
    {
        cout << cap->value << ' ';
        cap = cap->next;
    }
    cout << endl;
}

void insert_front_of_list( long long x, List* &cap )
{
    List *new_element = new List;

    new_element->next = cap;
    new_element->value = x;

    cap = new_element;
}

bool find_in_list( long long x, List* cap )
{
    while( cap != NULL )
    {
        if( cap->value == x )
        {
            return true;
        }
        cap = cap->next;
    }
    return false;
}

void remove_from_list( long long x, List* &cap )
{
    List *it = cap;
    if( cap == NULL )
    {
        return;
    }

    if( cap->value == x )
    {
        cap = cap->next;
        delete it;
    }
    while( it->next != NULL )
    {
        if( it->next->value == x )
        {
            List* aux = it->next;
            it->next = it->next->next;
            delete aux;
            return;
        }
        it = it->next;
    }
}

int hash_function( long long x )
{
    return x % HASH_SIZE;
}

int main()
{
    List *aux = NULL;
    vector<List *> hash_table( HASH_SIZE, aux );

    int n, operatie, key;
    long long int element;
    fscanf( f, "%d", &n );

    for( int i = 0; i < n; i++ )
    {
        fscanf( f, "%d%lld", &operatie, &element );
        key = hash_function( element );

        switch( operatie )
        {
        case 1:
            if( find_in_list( element, hash_table[key] ) == false )
            {
                insert_front_of_list( element, hash_table[key] );
            }
            break;

        case 2:
            remove_from_list( element, hash_table[key] );
            break;

        case 3:
            fprintf( g, "%d\n", find_in_list( element, hash_table[key] ) );
            break;
        }
    }

    fclose( f );
    fclose( g );
    return 0;
}