Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <list>
- using namespace std;
- class HashTable {
- vector<list<int>> table;
- int hash_f(int value) {
- value *= value;
- value >>= 11;
- return value % 1024;
- }
- public:
- HashTable() {
- table.resize(100000);
- }
- void add(int item) {
- int h = hash_f(item);
- table[h].push_front(item);
- }
- int find(int item) {
- int cur;
- list<int>::iterator p_begin;
- int h = hash_f(item);
- p_begin = table[h].begin();
- while (p_begin != table[h].end()) {
- cur = *p_begin;
- if (cur == item) {
- return 1;
- }
- advance(p_begin, 1);
- }
- return 0;
- }
- void del(int item) {
- int h = hash_f(item);
- table[h].remove(item);
- }
- };
- class Set {
- HashTable t;
- public:
- void ad(int item) {
- if (!t.find(item)) {
- t.add(item);
- }
- }
- int exists(int item) {
- return t.find(item);
- }
- void dell(int item) {
- t.del(item);
- }
- };
- int main() {
- int k;
- string s1;
- Set s;
- freopen("set.in", "r", stdin);
- freopen("set.out", "w", stdout);
- while (cin >> s1) {
- if (s1 == "insert") {
- cin >> k;
- s.ad(k);
- } else if (s1 == "exists") {
- cin >> k;
- if (s.exists(k)) {
- cout << "true\n";
- } else {
- cout << "false\n";
- }
- } else if (s1 == "delete") {
- cin >> k;
- s.dell(k);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement