Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <cmath>
- using namespace std;
- ifstream input("set.in", ios_base::in);
- ofstream output("set.out", ios_base::out | ios_base::trunc);
- struct Node {
- Node() {
- }
- Node(int value) {
- this->value = value;
- }
- int value;
- Node *next;
- };
- struct HashTable {
- HashTable() {
- hastTable.assign(SIZE + 3, nullptr);
- }
- int getHash(int value) {
- long a = value * 113 + 145861;
- a %= 6130667;
- a %= SIZE;
- return abs(a);
- }
- void insert(int value) {
- if (!contains(value)) {
- int hash = getHash(value);
- Node *currNode = hastTable[hash];
- if (currNode == nullptr) {
- hastTable[hash] = new Node(value);
- } else {
- while (currNode->next != nullptr){
- currNode = currNode->next;
- }
- currNode->next = new Node(value);
- }
- }
- }
- bool contains(int value) {
- int hash = getHash(value);
- Node *currNode = hastTable[hash];
- while (currNode != nullptr && currNode->value != value) {
- currNode = currNode->next;
- }
- return currNode != nullptr && currNode->value == value;
- }
- void remove(int value) {
- if (contains(value)) {
- int hash = getHash(value);
- Node *currNodePrev = hastTable[hash];
- if (currNodePrev->next != nullptr) {
- Node *currNode = hastTable[hash]->next;
- while (currNode->value != value) {
- currNodePrev = currNodePrev->next;
- currNode = currNode->next;
- }
- if (currNode->next == nullptr) {
- currNodePrev->next = nullptr;
- } else {
- currNodePrev->next = currNode->next;
- }
- } else {
- hastTable[hash] = nullptr;
- }
- }
- }
- const int SIZE = 987911;
- vector<Node *> hastTable;
- };
- int main() {
- string command;
- int value;
- HashTable hashtable;
- while (input >> command) {
- input >> value;
- if (command == "insert") {
- hashtable.insert(value);
- } else if (command == "exists") {
- output << (hashtable.contains(value) ? "true" : "false") << "\n";
- } else if (command == "delete") {
- hashtable.remove(value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement