Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #define IN_FILE_NAME "set.in"
- #define OUT_FILE_NAME "set.out"
- class IntList {
- private:
- class ListElement {
- public:
- int value = 0;
- ListElement* p_prev = nullptr;
- ListElement* p_next = nullptr;
- };
- ListElement* p_begin = nullptr;
- ListElement* p_end = nullptr;
- long list_size = 0;
- ListElement* find(int _value);
- public:
- ~IntList();
- void pushBack(int _value);
- bool isExist(int _value);
- void remove(int _value);
- long size();
- };
- class IntHashTable {
- private:
- IntList* table = nullptr;
- long table_size;
- const double A = (sqrt(5) - 1) / 2;
- long hash(int key);
- public:
- explicit IntHashTable(long size);
- ~IntHashTable();
- void add(int key);
- bool isExist(int key);
- void remove(int key);
- };
- // IntList class methods
- IntList::ListElement *IntList::find(int _value) {
- if (!list_size)
- return nullptr;
- ListElement *p_look = p_begin;
- while (p_look != nullptr && p_look->value != _value)
- p_look = p_look->p_next;
- return p_look;
- }
- IntList::~IntList() {
- ListElement* p_look = p_begin;
- while (p_look != nullptr) {
- ListElement* temp = p_look;
- p_look = p_look->p_next;
- delete(temp);
- }
- p_begin = nullptr;
- p_end = nullptr;
- }
- void IntList::pushBack(int _value) {
- list_size++;
- auto* temp = new ListElement;
- temp->value = _value;
- if (p_end == nullptr) {
- p_begin = temp;
- p_end = temp;
- return;
- }
- temp->p_prev = p_end;
- p_end->p_next = temp;
- p_end = temp;
- }
- bool IntList::isExist(int _value) {
- return find(_value) != nullptr;
- }
- void IntList::remove(int _value) {
- ListElement* ptr_temp = find(_value);
- if (ptr_temp == nullptr)
- return;
- if (ptr_temp == p_begin)
- p_begin = ptr_temp->p_next;
- if (ptr_temp == p_end)
- p_end = ptr_temp->p_prev;
- if (ptr_temp->p_prev != nullptr)
- ptr_temp->p_prev->p_next = ptr_temp->p_next;
- if (ptr_temp->p_next != nullptr)
- ptr_temp->p_next->p_prev = ptr_temp->p_prev;
- delete(ptr_temp);
- --list_size;
- }
- long IntList::size() {
- return list_size;
- }
- // IntHashTable class methods
- long IntHashTable::hash(int key) {
- if (key < 0)
- key = -key;
- double int_part;
- return (long)(table_size * modf(key * A, &int_part));
- }
- IntHashTable::IntHashTable(long size) {
- table = new IntList[size];
- table_size = size;
- }
- IntHashTable::~IntHashTable() {
- delete[](table);
- table = nullptr;
- }
- void IntHashTable::add(int key) {
- long hash_value = hash(key);
- if (!table[hash_value].isExist(key))
- table[hash_value].pushBack(key);
- }
- bool IntHashTable::isExist(int key) {
- return table[hash(key)].isExist(key);
- }
- void IntHashTable::remove(int key) {
- table[hash(key)].remove(key);
- }
- using std::ifstream;
- using std::ofstream;
- using std::string;
- int main() {
- ifstream fin(IN_FILE_NAME);
- if (!fin.is_open())
- return 2;
- ofstream fout(OUT_FILE_NAME);
- if (!fout.is_open())
- return 2;
- IntHashTable table(50000);
- while (true) {
- string command;
- fin >> command;
- if (command.empty())
- break;
- if (command == "insert") {
- int value;
- fin >> value;
- table.add(value);
- } else if (command == "delete") {
- int value;
- fin >> value;
- table.remove(value);
- } else if (command == "exists") {
- int value;
- fin >> value;
- if (table.isExist(value))
- fout << "true\n";
- else
- fout << "false\n";
- }
- }
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement