Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- const int lengthArr = 80021; // 9973, 9629, 8699, 7013, 6949
- ifstream in;
- ofstream out;
- struct node {
- struct node* prev;
- struct node* next;
- int key;
- };
- struct node* new_list() {
- node* x = (node*) malloc(sizeof(node));
- x->key = NULL;
- x->next = nullptr;
- return x;
- }
- struct node push_back (struct node** head, int value) {
- node* new_list = (struct node*)malloc(sizeof(node));
- new_list->key = value;
- (*head)->next = new_list;
- new_list->prev = *head;
- new_list->next = nullptr;
- *head = new_list;
- }
- struct node* search (struct node* head, int value) {
- struct node* cur = head;
- while (cur->next != nullptr && cur->key != value) {
- cur = cur->next;
- }
- return cur;
- }
- int pop(struct node **head) {
- node* prev = nullptr;
- int val;
- if (head == nullptr) {
- return NULL;
- }
- prev = (*head);
- val = prev->key;
- (*head) = (*head)->next;
- free(prev);
- return val;
- }
- struct node* Search(struct node* head, int key) {
- while (head->next != nullptr && head->key != key) {
- head = head->next;
- }
- return head;
- }
- void deleteN(struct node *head, int key) {
- struct node* find = head;
- struct node* point = Search(find, key);
- int var = NULL;
- if (point != nullptr && point->key == key) {
- printf ("i`m here1\n");
- if (point->next != nullptr)
- (point->next)->prev = point->prev;
- else if (point->prev != nullptr) {
- point->prev->next = nullptr;
- }
- printf ("i`m here2\n");
- if (point->prev != nullptr)
- (point->prev)->next = point->next;
- else if (point->next != nullptr) {
- point->next->prev = nullptr;
- }
- printf ("i`m here3\n");
- var = point->key;
- printf ("i`m here4\n");
- free(point);
- printf ("i`m here5\n");
- }
- }
- int hashFunction(int key) {
- int ans = 0;
- int mn = 1;
- while (key > 0) {
- ans += (key % 10) * mn;
- mn *= 17;
- key /= 10;
- }
- return ans % lengthArr;
- }
- int main() {
- in.open("set.in");
- out.open("set.out");
- struct node* arr[lengthArr];
- for (int i = 0; i < lengthArr; i++) {
- arr[i] = new_list();
- }
- string s;
- int ind = 0;
- while(in >> s) {
- int k;
- in >> k;
- int hash = hashFunction(k);
- if (s == "insert") {
- struct node* point = search(arr[hash], k);
- if (point->key != k) {
- push_back(&point, k);
- }
- }
- else if (s == "delete") {
- deleteN(arr[hash], k);
- }
- else {
- struct node* point = search(arr[hash], k);
- if (point->key != k)
- out << "false" << endl;
- else
- out << "true" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement