1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| /*
Задан набор записей следующей структуры:
ФИО студента, номер студенческого билета, список несданных зачетов.
По заданному номеру студенческого найти остальные сведения.
*/
#include <iostream>
#include <string>
#define PRIME_SIZE 29
using namespace std;
class Person{
public:
int num;
string name;
string surname;
string patronymic;
string zachet;
Person *next;
Person()
{
this->next = NULL;
}
Person (string name, string surname, string patronymic, string zachet, int num = 0)
{
this->name = name;
this->surname = surname;
this->patronymic = patronymic;
this->zachet = zachet;
this->num = num;
this->next = NULL;
}
~Person()
{
cout << "Delete " << this->name << endl;
if ( this->next != NULL )
{
delete this->next;
}
}
};
class HashTable // Хеш-таблица, представленная в виде массива элементов (которые в свою очередь представляют список).
{
Person *table[PRIME_SIZE];
// получает остаток от деления.
static int hash ( int num )
{
int a, i;
for ( a = i = 0; i < num; i++ )
{
a += i;
}
return a % PRIME_SIZE;
}
public:
HashTable()
{
for ( int i = 0; i < PRIME_SIZE; i++ )
{
table[i] = NULL;
}
}
~HashTable()
{
//cout << "Delete table\n";
for ( int i = 0; i < PRIME_SIZE; i++ )
{
delete table[i];
}
}
void add (string name, string surname, string patronymic, string zachet, int num )
{
int hashNumber = hash(num);
Person *pers = new Person(name, surname,patronymic, zachet, num);
Person *place = table[hashNumber];
if ( place == NULL )
{
table[hashNumber] = pers;
return;
}
while ( place->next != NULL )
{
place = place->next;
}
place->next = pers;
}
Person* find ( int num )
{
int hashNumber = hash(num);
Person *result = table[hashNumber];
if ( !result )
{
cout << "Element not found" << endl;
return NULL;
}
while ( result->num != num )
{
if ( !result->next )
{
cout << "Element not found" << endl;
return NULL;
}
result = result->next;
}
return result;
}
};
int main() {
setlocale(LC_ALL, "Rus");
HashTable newTable;
newTable.add ("Islam", "Isayev", "Aydinovich", "fdf", 65648 );
newTable.add ("Ruslan", "Isayev", "Aydinovich", "fgrg", 43566 );
newTable.add ("Maxsim", "Isayev", "Aydinovich", "rgreg", 34546 );
Person * search = newTable.find(65648);
if ( search )
{
cout << search->name<<" ";
cout << search->surname<<" ";
cout << search->patronymic <<" ";
}
return 0;
} |