Pagini recente » Cod sursa (job #196718) | Cod sursa (job #2651460) | Clasamentul arhivei educationale | Borderou de evaluare (job #2473721) | Cod sursa (job #3179423)
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int key;
struct Node *next;
}Node;
void AddToFrontList(Node **head,int key){
if(NULL==(*head)){
(*head)= (Node*)malloc(sizeof(Node));
(*head)->key=key;
(*head)->next=NULL;
return;
}
Node *temp=(Node *) malloc(sizeof(Node));
temp->key=key;
temp->next=(*head);
(*head)=temp;
}
void DeleteByKey(Node **head,int key){
if(NULL==(*head))
return;
Node *prev_node=NULL;
Node *curr_node=(*head);
while(NULL!=curr_node && curr_node->key!=key) {
prev_node=curr_node;
curr_node = curr_node->next;
}
if(NULL==curr_node)
return;
if(curr_node==(*head)){
(*head)=(*head)->next;
free(curr_node);
curr_node=NULL;
return;
}
prev_node->next=curr_node->next;
free(curr_node);
curr_node=NULL;
}
bool SearchByKey(Node *head,int key){
if(NULL==head)
return false;
while(head!=NULL && head->key!=key)
head=head->next;
if(NULL==head)
return false;
return true;
}
int HashFunction(int key,int sizeHashTable){
return key%sizeHashTable;
}
void PrintList(Node *head){
while(NULL!=head){
printf("%d ",head->key);
head=head->next;
}
}
int main() {
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
int sizeHashTable=200003;//100069;
Node **HashTable=(Node **) malloc(sizeof(Node*)*sizeHashTable);
for(int i=0;i<sizeHashTable;i++){
HashTable[i]=NULL;
}
int op,q,x;
int index;
scanf("%d",&q);
for(int i=0;i<q;i++){
scanf("%d",&op);
scanf("%d",&x);
index= HashFunction(x,sizeHashTable);
if(!SearchByKey(HashTable[index],x)){
switch (op) {
case 1:
AddToFrontList(&HashTable[index],x);
break;
case 3:
printf("0\n");
break;
}
}
else {
switch (op) {
case 2:
DeleteByKey(&HashTable[index],x);
break;
case 3:
printf("1\n");
break;
}
}
// printf("Hashtable[%d]:",index);
// PrintList(HashTable[index]);
// printf("\n");
}
for(int i=0;i<100069;i++)
free(HashTable[i]);
free(HashTable);
return 0;
}