-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini database - hashTable ver.cpp
189 lines (165 loc) · 4.42 KB
/
mini database - hashTable ver.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Created by Jinho on 2023-05-13.
//
#include <iostream>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstring>
#include <locale.h>
#include <stdlib.h>
#include <algorithm>
#define INIT_STUDENT_NUM 10
#define BUCKET_COUNT 1 << 10
#define DIV (BUCKET_COUNT - 1)
#define MAX_ARRAY_NUMBER 100
using namespace std;
class Student {
public:
string name;
int score;
Student *next;
Student() {
name = "head";
score = -1;
next = nullptr;
}
Student(string _name, int _score) {
name = _name;
score = _score;
next = nullptr;
}
};
class Students {
public:
Students() {
initBucket();
}
void insertRecord(string &name, int score);
void deleteByName(string &name);
void listByName(string &name);
void printStudentsInfo();
private:
int studentSize;
Student hashTable[BUCKET_COUNT];
void generateMockStudent(int studentCount);
Student *findPrevStudent(string &name);
int getKey(string &name);
void initBucket();
};
void Students::initBucket() {
studentSize = 0;
for (int i = 0; i < BUCKET_COUNT; i++) {
hashTable[i].next = nullptr;
}
generateMockStudent(INIT_STUDENT_NUM);
}
void Students::insertRecord(string &name, int score) {
Student *newStudent = new Student(name, score);
int key = getKey(name);
Student *ptr = &hashTable[key];
while (ptr->next) {
ptr = ptr->next;
}
ptr->next = newStudent;
studentSize++;
}
void Students::deleteByName(string &name) {
Student *ptr = findPrevStudent(name);
if (ptr->next == nullptr) {
cout << "there is no user name '" << name << "'\n";
return;
}
cout << "delete user name : " << ptr->next->name << ", score : " << ptr->next->score << '\n';
ptr->next = ptr->next->next;
studentSize--;
}
void Students::listByName(string &name) {
Student* ptr;
ptr = findPrevStudent(name);
if (ptr->next == nullptr) {
cout << "there is no user name '" << name << "'\n";
return;
}
cout << "find user name : " << ptr->next->name << ", score : " << ptr->next->score << '\n';
}
void Students::generateMockStudent(int studentCount) {
int randNumTemp;
string nameTemp;
for (int i = 0; i < studentCount; i++) {
randNumTemp = rand() % 8 + 1;
nameTemp = "";
// 아스키 97 ~ 122
for (int j = 0; j < randNumTemp; j++) {
nameTemp += rand() % 25 + 97;
}
insertRecord(nameTemp, rand() % 100);
}
}
void Students::printStudentsInfo() {
cout << "============ student count is " << studentSize << "]! print student info ============" << '\n';
Student *ptr;
int cnt = 1;
for (int i = 0; i < BUCKET_COUNT; i++) {
if (!hashTable[i].next) continue;
ptr = hashTable[i].next;
while (ptr) {
cout << cnt++ << ". name : " << ptr->name << ", score : " << ptr->score << '\n';
ptr = ptr->next;
}
}
}
int Students::getKey(string &name) {
long long key = 5381;
for (char ch : name) {
key = ((key * 26) + key) + ch - 'a' + 1;
}
return (int) (key & DIV);
}
Student *Students::findPrevStudent(string &name) {
int key = getKey(name);
Student *ptr = &hashTable[key];
while (ptr->next) {
if (ptr->next->name == name) {
return ptr;
}
ptr = ptr->next;
}
return ptr;
}
int main() {
srand((unsigned int) (time(0)));
string command;
int scoreBuffer;
string nameBuffer;
string insert = "insert";
string listName = "listName";
string deleteByName = "delete";
string print = "print";
string exit = "exit";
Students students = Students();
while (1) {
command = "";
cin >> command;
cout << command << '\n';
if (command == insert) {
cin >> nameBuffer >> scoreBuffer;
students.insertRecord(nameBuffer, scoreBuffer);
} else if (command == listName) {
cin >> nameBuffer;
students.listByName(nameBuffer);
} else if (command == deleteByName) {
cin >> nameBuffer;
students.deleteByName(nameBuffer);
} else if (command == print) {
students.printStudentsInfo();
} else if (command == exit) {
break;
} else {
printf("there is no command like that\n");
}
}
}