Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int num;
- string com;
- string secname;
- Node* next;
- Node(int _num, string _com, string _secname) : num(_num), com(_com), secname(_secname), next(nullptr) {}
- };
- struct list {
- Node* first;
- Node* last;
- list() : first(nullptr), last(nullptr) {}
- bool is_empty() {
- return first == nullptr;
- }
- void push_back(int _num, string _com, string _secname) {
- Node* p = new Node(_num, _com, _secname);
- if (is_empty()) {
- first = p;
- last = p;
- return;
- }
- last->next = p;
- last = p;
- }
- void print() {
- if (is_empty()) return;
- Node* p = first;
- while (p) {
- cout << p->num << "|" << p->com << "|" << p->secname << " ---->";
- p = p->next;
- }
- cout << endl;
- }
- Node* find(int _num) {
- Node* p = first;
- while (p && p->num != _num) p = p->next;
- cout << p->num << "|" << p->com << "|" << p->secname;
- return (p && p->num == _num) ? p : nullptr;
- }
- void remove_first() {
- if (is_empty()) return;
- Node* p = first;
- first = p->next;
- delete p;
- }
- void remove_last() {
- if (is_empty()) return;
- if (first == last) {
- remove_first();
- return;
- }
- Node* p = first;
- while (p->next != last) p = p->next;
- p->next = nullptr;
- delete last;
- last = p;
- }
- void remove(int _num) {
- if (is_empty()) return;
- if (first->num == _num) {
- remove_first();
- return;
- }
- else if (last->num == _num) {
- remove_last();
- return;
- }
- Node* slow = first;
- Node* fast = first->next;
- while (fast && fast->num != _num) {
- fast = fast->next;
- slow = slow->next;
- }
- if (!fast) {
- cout << "This element does not exist" << endl;
- return;
- }
- slow->next = fast->next;
- delete fast;
- }
- };
- class HashTable {
- private:
- list* l = new list[20];
- int Num;
- string Fio;
- string Adress;
- public:
- HashTable() {
- for (int i = 0; i < 5; i++) {
- cin >> Num >> Fio >> Adress;
- int id = hashFunction(Num); // Находим индекс элемента в массиве
- cout << id;
- l[id].push_back(Num, Fio, Adress); // добавляем в список
- }
- }
- int hashFunction(int _num) {
- return _num % 10;
- }
- void printTable() { // метод вывода таблицы
- for (int i = 0; i < 20; i++) {
- l[i].print();
- }
- cout << endl;
- }
- void insertItem(int _num, string _com, string _secname) { // метод вставки элементов
- int id = hashFunction(_num);
- l[id].push_back(_num, _com, _secname);
- }
- void deleteItem(int _num) { // метод удаления элементов
- int id = hashFunction(_num);
- l[id].remove(_num);
- }
- void findItem(int _num) { // метод поиска элементов
- int id = hashFunction(_num);
- l[id].find(_num);
- cout << endl;
- }
- };
- int main()
- {
- int a, b, c;
- HashTable hash;
- hash.printTable();
- cin >> a;
- cout << "Insert" << endl;
- hash.insertItem(a, "5", "7");
- hash.printTable();
- cin >> b;
- cout << "Find" << endl;
- hash.findItem(b);
- cin >> c;
- cout << "Delete" << endl;
- hash.deleteItem(c);
- hash.printTable();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement