Cod sursa(job #1474083)

Utilizator glbglGeorgiana bgl glbgl Data 20 august 2015 21:34:50
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>

#define MAX 1000003


using namespace std;

typedef struct Cell{
	int info;
	Cell *urm;
} List, *AList;

typedef struct{
	AList *lists;
} Hash;

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

int N;

int HashFunction(int x){

	int h = x % MAX;
	return h;
}


AList * CheckIntoHash(AList *al, int x){

	while((*al) != NULL){
		if((*al)->info == x){
			return al;
		}
		al = &(*al)->urm;
	}

	return NULL;
}


void AddToHash(AList *al, int x){

	if(CheckIntoHash(al, x) != NULL)
		return;

	AList aux = (AList)malloc(sizeof(List));

	aux->info = x;

	aux->urm = *al;
	*al = aux;
}

void RemoveFromHash(AList *al, int x){

	if(CheckIntoHash(al, x) == NULL)
		return;

	AList aux = *al;
	*al = (*al)->urm;
	free(aux);
}


void readFromFile(){

	in >> N;

	Hash hash;
	hash.lists = (AList *)calloc(MAX, sizeof(AList));

	int op, x;
	for(int i = 0; i < N; ++i){
		in >> op >> x;

		int h = HashFunction(x);

		if(op == 1)
			AddToHash(&hash.lists[h], x);
		if(op == 2)
			RemoveFromHash(&hash.lists[h], x);
		if(op == 3){
			if(CheckIntoHash(&hash.lists[h], x) == NULL)
				out << 0 << "\n";
			else out << 1 << "\n";
		}
	}
}


int main(){

	readFromFile();
	return 0;
}