Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <iostream>
- //#include <list>
- //#include <fstream>
- //#include <vector>
- //#include <stdio.h>
- //#include <stdlib.h>
- //#include <time.h>
- //#include <algorithm>
- //
- //using namespace std;
- //
- //class Hash
- //{
- // int bucket;
- // list<int>* table;
- //public:
- // Hash(int v);
- // void AddItem(int value);
- // void DeleteItem(int key);
- // int HashFunc(int value);
- // void DisplayHash();
- //};
- //
- //Hash::Hash(int v)
- //{
- // this->bucket = v;
- // table = new list<int>[bucket];
- //}
- //
- //void Hash::AddItem(int key)
- //{
- // int index = HashFunc(key);
- // table[index].push_back(key);
- //}
- //
- //void Hash::DeleteItem(int key)
- //{
- // int index = HashFunc(key);
- // list <int>::iterator i;
- // for (i = table[index].begin(); i != table[index].end(); i++)
- // {
- // if (*i == key)
- // break;
- // }
- // if (i != table[index].end())
- // table[index].erase(i);
- //}
- //
- //int Hash::HashFunc(int key)
- //{
- // return (key % bucket);
- //}
- //
- //void Hash::DisplayHash()
- //{
- // for (int i = 0; i < bucket; i++)
- // {
- // cout << i;
- // for (auto x : table[i])
- // {
- // cout << " --> " << x;
- // }
- // cout << endl;
- // }
- //}
- //
- //void Random(int n)
- //{
- // srand(time(NULL));
- // ofstream out;
- // out.open("input.txt");
- // int v;
- // for (int i = 0; i < n; i++)
- // {
- // v = rand() % (100 + 1);
- // if (i != n)
- // out << v << " ";
- // else
- // {
- // out << v;
- // }
- // }
- // out.close();
- //}
- //
- //void Input(Hash hashtable)
- //{
- // vector<int> vec;
- // ifstream inp;
- // inp.open("input.txt");
- // int x;
- // while (inp >> x)
- // {
- // vec.push_back(x);
- // }
- // sort(vec.begin(), vec.end());
- // for (auto i = vec.begin(); i != vec.end(); i++)
- // {
- // hashtable.AddItem(*i);
- // }
- //}
- //
- //int main()
- //{
- // /*vector<int> vec;
- // for (int i = 0; i < 10; i++)
- // vec.push_back(i);
- // for (auto j = vec.begin(); j != vec.end(); j++)
- // cout << *j << " ";*/
- // int m, n;
- // cout << "Nhap so bucket: ";
- // cin >> m;
- // cout << "Nhap so phan tu can random: ";
- // cin >> n;
- // Random(n);
- // Hash hashtable(m);
- // Input(hashtable);
- // hashtable.DisplayHash();
- // return 0;
- //}
- // ======================================Viết lại=========================================
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <list>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- using namespace std;
- class HashTable
- {
- int bucket;
- list<int>* hashtab;
- public:
- HashTable(int v);
- int HashFunc(int value);
- void AddItem(int value);
- void DeleteItem(int value);
- void DisplayHash();
- };
- HashTable::HashTable(int v)
- {
- this->bucket = v;
- hashtab = new list<int>[bucket];
- }
- int HashTable::HashFunc(int value)
- {
- return (value % bucket);
- }
- void HashTable::AddItem(int value)
- {
- int index = HashFunc(value);
- hashtab[index].push_back(value);
- }
- void HashTable::DeleteItem(int value)
- {
- int index = HashFunc(value);
- list<int> ::iterator i;
- for (i = hashtab[index].begin(); i != hashtab[index].end(); i++)
- {
- if (*i == value)
- break;
- }
- if (i != hashtab[index].end())
- hashtab[index].erase(i);
- }
- void HashTable::DisplayHash()
- {
- for (int i = 0; i < bucket; i++)
- {
- cout << i;
- for (auto x : hashtab[i])
- {
- cout << "-->" << x;
- }
- cout << endl;
- }
- }
- void Input(HashTable hash)
- {
- ifstream inp;
- int x;
- inp.open("input.txt");
- vector<int> vec;
- if (!inp.is_open())
- cout << "ERROR: Cannot open the input file!";
- else
- {
- while (inp >> x)
- {
- vec.push_back(x);
- }
- }
- sort(vec.begin(), vec.end());
- for (auto i = vec.begin(); i != vec.end(); i++)
- {
- hash.AddItem(*i);
- }
- }
- int main()
- {
- int m;
- cout << "Nhap so bucket: ";
- cin >> m;
- HashTable Hash(m);
- Input(Hash);
- Hash.DeleteItem(27);
- Hash.DeleteItem(99);
- Hash.DisplayHash();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement