Cod sursa(job #1267490)

Utilizator codebreaker24Tivadar Ionut codebreaker24 Data 19 noiembrie 2014 22:55:03
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
//Exemplificare structuri hash
# include <fstream>
# include <iostream>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

const int N = 1000033;
struct lnod
{
     int val;

     lnod *next = NULL;

};

lnod h[N];

inline int hash_function( int x );
void add_node (int  x );
void delete_node(int x);
lnod* find_node(int x);






int main ()
{
     int i;
     int k;
     int tip, value;
     fin >> k;


     for(i=0; i<k; i++)
     {

         fin >> tip >> value;
         if(tip == 1 )
            add_node(value);
         if(tip == 2 )
            delete_node(value);
         if(tip == 3 )
         {
             if(!find_node(value))
                fout << "0\n";
             else
                fout << "1\n";

         }
     }




};

inline int hash_function(int x )
{

    return x % N;
}
void add_node (int x)
{
    int n = hash_function(x);
    if(!find_node(x))
    {
    lnod *nod_nou = new lnod;
    nod_nou->val = h[n].val;
    nod_nou->next = h[n].next;

        h[n].val = x;
        h[n].next =nod_nou;
    }


}
void delete_node(int x)
{
   int n = hash_function(x);
   lnod *p  = new lnod;
   p = (h+n);

   while (p!=NULL)
   {
      if (p->val== x)
      {
        p->val = (p->next)->val;
        p->next =(p->next)->next;
        return;
      }
      p = p->next;
}
}
lnod* find_node (int x )
{
     int n= hash_function(x);

     lnod *p = new lnod;
       p = (h+n);
     while (p != NULL)
     {


         if (p->val == x  )
            return p;
         else
            p = p->next;
     }
     return 0;


}