Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct element {
  6.     int Value;
  7.     struct element * next;
  8. }node;
  9.  
  10. node *hashtable[1000007];
  11.  
  12. void addNode(int value){
  13.     int key;
  14.     node *n, *n1;
  15.     n = (node *)malloc(sizeof(node));
  16.     n->Value = value;
  17.     n->next = NULL;
  18.     key = abs( value % 1000007 );
  19.     if(hashtable[key] == NULL)
  20.         hashtable[key] = n;
  21.     else {
  22.         for(n1=hashtable[key];n1->next!=NULL;n1=n1->next);
  23.             n1->next = n;
  24.     }
  25. }
  26.  
  27. int searchNode(int value){
  28.     int key = abs( value % 1000007 );
  29.     node *n;
  30.     for(n=hashtable[key];n!=NULL;n=n->next){
  31.         if(n->Value == value)
  32.             return 1;
  33.     }
  34.     return 0;
  35. }
  36.  
  37. void deleteNode(int value) {
  38.     int key = abs( value % 1000007 );
  39.     node *n, *n1;
  40.     if(hashtable[key]->Value == value) {
  41.         n=hashtable[key];
  42.         hashtable[key] = hashtable[key]->next;
  43.         free(n);
  44.     return;
  45.     }
  46.     for(n=hashtable[key];n->next!=NULL; n=n->next){
  47.         n1 = n->next;
  48.         if(n1->Value == value){
  49.             n->next = n1->next;
  50.             free(n1);
  51.             break;
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. int main() {
  58.     for (int i=0; i<1000007; i++){
  59.         hashtable[i] = NULL;
  60.     }
  61.     string s;
  62.     int value, index;
  63.  
  64.     ifstream fin("set.in");
  65.     ofstream fout("set.out");
  66.     while ( fin >> s ){
  67.         fin >> value;
  68.         if ( s == "insert" ){
  69.             if(!searchNode(value))
  70.             addNode(value);
  71.         }
  72.         if ( s == "delete" ){
  73.             if(searchNode(value))
  74.             deleteNode (value);
  75.         }
  76.         if ( s == "exists" ){
  77.             if(searchNode(value))
  78.                 fout << "true" << endl;
  79.             else
  80.                 fout << "false" << endl;
  81.         }
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement