Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- struct comp
- {
- int data;
- comp* next;
- };
- comp** create(int m)
- {
- comp** H = new comp*[m];
- for (int i = 0; i < m; i++) H[i] = NULL;
- return H;
- }
- void add(int data, int m, comp** H)
- {
- comp* tmp = new comp;
- tmp->data = data;
- int i = data % m;
- if (H[i] == NULL)
- {
- H[i] = tmp;
- tmp->next = NULL;
- }
- else {
- tmp->next = H[i];
- H[i] = tmp;
- }
- }
- comp* search(int data, int m, comp** H)
- {
- int i = abs(data % m);
- comp* tmp = H[i];
- while (tmp != NULL)
- {
- if (tmp->data == data) return tmp;
- tmp = tmp->next;
- }
- return NULL;
- }
- void delet(int m, comp** H)
- {
- comp *tmp, *temp;
- for (int i = 0; i < m; i++)
- {
- cout << "H[" << i << "] = ";
- tmp = H[i];
- while (tmp != NULL)
- {
- cout << tmp->data << " ";
- temp = tmp;
- tmp = tmp->next;
- delete temp;
- }
- cout << endl;
- }
- delete[] H;
- }
- int main()
- {
- int n;
- cout << "n = ";
- cin >> n;
- int* mass = new int[n];
- int m;
- cout << "m = ";
- cin >> m;
- comp** H;
- H = create(m);
- cout << "mass[i] = ";
- for (int i = 0; i < n; i++){
- cin >> mass[i];
- }
- for (int i = 0; i < n; i++)
- {
- cout << " " << mass[i];
- }
- cout << endl;
- for (int i = 0; i < n; i++)
- {
- add(mass[i], m, H);
- }
- int key;
- comp* p;
- cout << "key = ";
- cin >> key;
- while (key != -1)
- {
- p = search(key, m, H);
- if (p == NULL) cout << "There is no element" << endl;
- else cout << p->data << endl;
- cin >> key;
- }
- delet(m, H);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement