Cod sursa(job #1112151)

Utilizator techLaurentiu Avasiloaie tech Data 19 februarie 2014 14:52:38
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.11 kb
#include <stdio.h>
#include <fstream>
#define a 4335
#define b 65464
#define modulo 666013

using namespace std;

ifstream in ( "hashuri.in" ) ;

struct node
{
    int value;
    node *next ;
};

struct hash_table
{
    int used ;
    node *next ;
};

hash_table vect[modulo+5] ;
node *p ;

int x ;
int n , tip , i , y , ok ;

void insert_hash ()
{
    node *q ;
    q = new node ; q->value = x ; q->next = NULL ;

    if ( vect[y].used == 0 ) { vect[y].next = q ; vect[y].used = 1 ; }

    else
    {
        p = vect[y].next ;

        while ( p->next != NULL  && p->value != x )
        {
            p = p->next ;
        }

        if ( p->value != x )
        {
            p->next = q ;
        }
    }

}

void delete_hash ()
{
    if ( vect[y].used )
    {
        if ( vect[y].next != NULL )
        {
            if ( vect[y].next->value == x )
            { vect[y].used = 0 ; }

            else {
                p = vect[y].next ; ok = 1 ;

                while ( p->next != NULL && ok )
                {
                    if ( p->next->value == x ) { ok = 0 ; }
                }

                if ( ok == 0 )
                {
                    if ( p->next->next != NULL ) { p->next = p->next->next ; }
                    else { p->next = NULL ; }
                }
            }
        }
    }
}

void call_hash ()
{
    if ( vect[y].used )
    {
        p = vect[y].next ;

        while ( p->value != x && p->next != NULL )
        {
                p = p->next ;
        }

        if ( p->value == x ) { printf ( "1\n" ) ; }
        else { printf ( "0\n" ) ; }
    }

    else
    {
        printf ( "0\n" ) ;
    }
}

int function_hash ()
{
    return ( ( a*x + b ) % modulo ) ;
}

int main()
{
    freopen ( "hashuri.out" , "w" , stdout ) ;

    in >> n ;

    for ( i = 1 ; i <= n ; i ++ )
    {
        in >> tip >> x ;

        y = function_hash() ;

        if ( tip == 1 ) { insert_hash() ; }
        else if ( tip == 2 ) { delete_hash() ; }
        else { call_hash() ; }
    }

    return 0;
}