Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. const int lengthArr = 80021; // 9973, 9629, 8699, 7013, 6949
  8.  
  9. ifstream in;
  10. ofstream out;
  11.  
  12. struct node {
  13.   struct node* prev;
  14.   struct node* next;
  15.   int key;
  16. };
  17.  
  18. struct node* new_list() {
  19.   node* x = (node*) malloc(sizeof(node));
  20.   x->key = NULL;
  21.   x->next = nullptr;
  22.   return x;
  23. }
  24.  
  25. struct node push_back (struct node** head, int value) {
  26.   node* new_list = (struct node*)malloc(sizeof(node));
  27.   new_list->key = value;
  28.   (*head)->next = new_list;
  29.   new_list->prev = *head;
  30.   new_list->next = nullptr;
  31.   *head = new_list;
  32. }
  33.  
  34. struct node* search (struct node* head, int value) {
  35.   struct node* cur = head;
  36.   while (cur->next != nullptr && cur->key != value) {
  37.     cur = cur->next;
  38.   }
  39.   return cur;
  40. }
  41.  
  42. int pop(struct node **head) {
  43.   node* prev = nullptr;
  44.   int val;
  45.   if (head == nullptr) {
  46.     return NULL;
  47.   }
  48.   prev = (*head);
  49.   val = prev->key;
  50.   (*head) = (*head)->next;
  51.   free(prev);
  52.   return val;
  53. }
  54.  
  55. struct node* Search(struct node* head, int key) {
  56.   while (head->next != nullptr && head->key != key) {
  57.     head = head->next;
  58.   }
  59.   return head;
  60. }
  61.  
  62. void deleteN(struct node *head, int key) {
  63.   struct node* find = head;
  64.   struct node* point = Search(find, key);
  65.   int var = NULL;
  66.   if (point != nullptr && point->key == key) {
  67.     printf ("i`m here1\n");
  68.     if (point->next != nullptr)
  69.       (point->next)->prev = point->prev;
  70.     else if (point->prev != nullptr) {
  71.       point->prev->next = nullptr;
  72.     }
  73.     printf ("i`m here2\n");
  74.     if (point->prev != nullptr)
  75.       (point->prev)->next = point->next;
  76.     else if (point->next != nullptr) {
  77.       point->next->prev = nullptr;
  78.     }
  79.     printf ("i`m here3\n");
  80.     var = point->key;
  81.     printf ("i`m here4\n");
  82.     free(point);
  83.     printf ("i`m here5\n");
  84.   }
  85. }
  86.  
  87. int hashFunction(int key) {
  88.   int ans = 0;
  89.   int mn = 1;
  90.   while (key > 0) {
  91.     ans += (key % 10) * mn;
  92.     mn *= 17;
  93.     key /= 10;
  94.   }
  95.   return ans % lengthArr;
  96. }
  97.  
  98.  
  99. int main() {
  100.   in.open("set.in");
  101.   out.open("set.out");
  102.   struct node* arr[lengthArr];
  103.   for (int i = 0; i < lengthArr; i++) {
  104.     arr[i] = new_list();
  105.   }
  106.   string s;
  107.   int ind = 0;
  108.   while(in >> s) {
  109.     int k;
  110.     in >> k;
  111.     int hash = hashFunction(k);
  112.     if (s == "insert") {
  113.       struct node* point = search(arr[hash], k);
  114.       if (point->key != k) {
  115.         push_back(&point, k);
  116.       }
  117.     }
  118.     else if (s == "delete") {
  119.       deleteN(arr[hash], k);
  120.     }
  121.     else {
  122.       struct node* point = search(arr[hash], k);
  123.       if (point->key != k)
  124.         out << "false" << endl;
  125.       else
  126.         out << "true" << endl;
  127.     }
  128.   }
  129.   return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement