Pagini recente » Cod sursa (job #3216760) | Cod sursa (job #2389357) | Cod sursa (job #18952) | Cod sursa (job #3275893) | Cod sursa (job #2577925)
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode{
int value;
struct ListNode *next;
}ListNode;
typedef struct List{
struct ListNode *front;
struct ListNode *back;
}List;
List * createList(){
List *newList = (List *)calloc(1, sizeof(List));
return newList;
}
void addElem(List *list, int value){
if(list == NULL)
return;
ListNode *newNode = (ListNode *)calloc(1, sizeof(ListNode));
newNode->value = value;
if(list->front == NULL){
list->front = list->back = newNode;
}
else{
list->back->next = newNode;
list->back = newNode;
}
}
void delElem(List *list, int value){
if(list == NULL || list->front == NULL)
return;
if(list->front == list->back && list->front->value == value){
free(list->front);
list->front = list->back = NULL;
}
else{
ListNode *crtNode = list->front;
ListNode *antNode = NULL;
while(crtNode != NULL){
if(crtNode->value == value){
if(antNode == NULL){
list->front = crtNode->next;
}
else{
antNode->next = crtNode->next;
}
if(crtNode->next == NULL){
list->back = antNode;
}
free(crtNode);
}
antNode = crtNode;
crtNode = crtNode->next;
}
}
}
int checkIfExists(List *list, int value){
if(list == NULL)
return 0;
ListNode *crtNode = list->front;
while(crtNode != NULL){
if(crtNode->value == value)
return 1;
crtNode = crtNode->next;
}
return 0;
}
void destroyList(List *list){
if(list == NULL)
return;
ListNode *crtNode = list->front;
while(crtNode != NULL){
ListNode *aux = crtNode->next;
free(crtNode);
crtNode = aux;
}
free(list);
}
#define MOD 666013
int noTests, value, type;
List* HashTable[MOD];
void Initialize(){
for(int i = 0; i < MOD; ++ i)
HashTable[i] = createList();
}
void Destroy(){
for(int i = 0; i < MOD; ++ i)
destroyList(HashTable[i]);
}
int main(int argc, const char * argv[]) {
FILE* in = fopen("hashuri.in", "r");
FILE* out = fopen("hashuri.out", "w");
fscanf(in, "%d", &noTests);
Initialize();
for(int testCrt = 0; testCrt < noTests; ++ testCrt){
fscanf(in, "%d %d", &type, &value);
int valueMod = value % MOD;
switch(type){
case 1:
if(!checkIfExists(HashTable[valueMod], value)){
addElem(HashTable[valueMod], value);
}
break;
case 2:
delElem(HashTable[valueMod], value);
break;
case 3:
fprintf(out, "%d\n", checkIfExists(HashTable[valueMod], value));
}
}
Destroy();
return 0;
}