Pagini recente » Cod sursa (job #1130170) | Istoria paginii runda/laborator_10i/clasament | Cod sursa (job #409375) | Cod sursa (job #1877004) | Cod sursa (job #2225538)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node* next;
}node;
node* tabela[100003];
int hash(int key) {
return key % 100003;
}
int find(int key) {
int poz = hash(key);
node* p = tabela[poz];
if (p == NULL) {
return 0;
}
else {
while (p != NULL) {
if (p->key == key) {
return 1;
}
p = p->next;
}
return 0;
}
}
void insert(int key) {
if (find(key) == 1) {
return;
}
int poz = hash(key);
node* p = (node *)malloc(sizeof(node));
if (p == NULL) {
perror("malloc failed");
exit(1);
}
p->key = key;
p->next = tabela[poz];
tabela[poz] = p;
}
void remove(int key) {
if (find(key) == 0) {
return;
}
int poz = hash(key);
node* p = tabela[poz];
if (tabela[poz]->next == NULL) { // se sterge primul element din lista
tabela[poz] = NULL;
}
else {
while (p->next != NULL) {
if (p->next->key == key) {
node* q = p->next;
p->next = q->next;
q->next = NULL;
free(q);
return;
}
p = p->next;
}
}
}
int main() {
FILE* ip;
ip = fopen("hashuri.in", "r");
if (ip == NULL) {
perror("Could not open input file");
return 1;
}
FILE* op;
op = fopen("hashuri.out", "w");
if (op == NULL) {
perror("Could not open output file");
return 1;
}
int n;
fscanf(ip, "%d", &n);
for (int i = 0; i < n; i++) {
int oper, x;
fscanf(ip, "%d%d", &oper, &x);
if (oper == 1) {
insert(x);
}
else if (oper == 2) {
remove(x);
}
else {
fprintf(op, "%d\n", find(x));
}
}
return 0;
}