Cod sursa(job #1433705)

Utilizator k_ounu_eddyIacob Eduard k_ounu_eddy Data 9 mai 2015 18:20:53
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3 kb
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define DIM 666013

struct nod
{
int info;
nod* nod_urm;
} Nod;

void addNode(int _info, struct nod* &radacina)
{
    struct nod* n = radacina;
    bool gasit = false;
    if(n != NULL)
    {
        while(n->nod_urm && !gasit)
        {
            n = n->nod_urm;
            if(n->info == _info)
                gasit = true;
        }
        if(!gasit)
        {
            struct nod* nNou;
            nNou = new struct nod;
            nNou->info = _info;
            n->nod_urm = nNou;
            nNou->nod_urm = NULL;
        }
    } else
    {
        radacina = new struct nod;
        radacina->nod_urm = NULL;
        radacina->info = _info;
    }
}


void deleteNode(int _info, struct nod* &radacina)
{
    struct nod* n = radacina;
    struct nod* prev = NULL;
    bool bRes = true;
    bool gasit;

    if(n == NULL)
        bRes = false;


    if(bRes){
            if(n->info == _info)
                gasit = true;
        else
            gasit = false;
    }

    if(bRes)
    {
        while(n->info != _info && n!=NULL)
        {
            prev = n;
            n = n->nod_urm;        
        }
    }

    if(bRes)
    {
        if(n!=NULL)
            gasit = true;
    }

    if(bRes)
    {
        if(gasit)
        {
            if(prev != NULL)
            {
                prev->nod_urm = n->nod_urm;
                delete n;
            } else
            {
                radacina = radacina->nod_urm;
                delete n;
            }
        }
    }    
}

void list(struct nod* radacina)
{
    struct nod* n = radacina;
    while(n)
    {
        cout<<n->info<<" ";
        n = n->nod_urm;        
    }
    cout<<endl;
}

bool find( int _info, struct nod* radacina)
{
    bool gasit = false;
    while(radacina!=NULL && !gasit)
    {
        if(radacina->info == _info)
            gasit = true;
        else
            radacina = radacina->nod_urm;
    }

    return gasit;
}

int main()
{
    int N;
    fstream fin("hashuri.in");
    ofstream fout;
    fout.open("hashuri.out");
    struct nod* radacina[DIM];
    struct nod* head[DIM];

    memset(radacina, 0, DIM * sizeof(struct nod*));
    memset(head, 0, DIM * sizeof(struct nod*));    

    fin>>N;
    for(int i=0;i<N;i++)
    {
        int a,b;
        fin>>a>>b;
        if(a == 1)
        {
            addNode(b, radacina[b%DIM]);
        }
        else if(a == 2)
        {
            deleteNode(b, radacina[b%DIM]);
        } else if (a == 3)
        {
            if(find(b, radacina[b%DIM]))
            {
                #ifdef DEBUG
                    cout<<b<<" gasit"<<endl;
                #endif
                fout<<"1";
            }
            else
            {
                #ifdef DEBUG
                    cout<<b<<" negasit"<<endl;
                #endif

                fout<<"0";
            }
            if(i<N-1)
                fout<<endl;
        }
    }
    
    fin.close(); 
    fout.close();
    return 0;
}