Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- struct Element {
- int key;
- struct Element* link;
- };
- Element* hashTable[115249] = {nullptr};
- void insert(int key) {
- int index = abs(key % 115249);
- Element* ptr = new Element();
- ptr->key = key;
- ptr->link = nullptr;
- if (hashTable[index] == nullptr) {
- hashTable[index] = ptr;
- }
- else {
- Element* ptr1;
- ptr1 = hashTable[index];
- while (ptr1->link) {
- if (ptr1->key == key)
- return;
- ptr1 = ptr1->link;
- }
- if (ptr1->key != key)
- ptr1->link = ptr;
- }
- }
- void del(int key) {
- Element* ptr;
- int index = abs(key % 115249);
- ptr = hashTable[index];
- if (ptr == nullptr)
- return;
- if (ptr->key == key) {
- hashTable[index] = ptr->link;
- return;
- }
- while (ptr->link) {
- if (ptr->link->key == key) {
- ptr->link = ptr->link->link;
- return;
- }
- ptr = ptr->link;
- }
- }
- bool exists(int key) {
- Element* ptr;
- int index = abs(key % 115249);
- ptr = hashTable[index];
- if (!ptr) {
- return false;
- }
- if (ptr->key == key) {
- return true;
- }
- while (ptr->link) {
- if (ptr->key == key)
- return true;
- ptr = ptr->link;
- }
- return ptr->key == key;
- }
- int main() {
- ifstream fin("set.in");
- ofstream fout("set.out");
- string com;
- int key;
- while (fin >> com) {
- fin >> key;
- if (com == "insert")
- insert(key);
- else if (com == "delete")
- del(key);
- else if (com == "exists"){
- bool ex = exists(key);
- if (ex)
- fout << "true" << "\n";
- else
- fout << "false" << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement