-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable_basics.cpp
More file actions
141 lines (125 loc) · 3.38 KB
/
hashtable_basics.cpp
File metadata and controls
141 lines (125 loc) · 3.38 KB
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
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
struct HashTable{
int MAX_LENGTH = 4;
string * password = new string[MAX_LENGTH];
void intialize_hashtable(){
for (int i = 0; i < 4; i++){
password[i].clear();
}
}
bool isFull(){
bool full = true;
int count = 0;
for (int i = 0; i < MAX_LENGTH; i++){
if(password[i].empty()){
full = false;
}
}
return full;
}
int hashfunc(string user_name){
int sum = 0;
int hash = 0;
//add your code below
for (int i = 0; i < user_name.length(); i++){
sum += user_name[i];
}
hash = sum % MAX_LENGTH;
return hash;
}
bool is_slot_empty(int hash){
bool empty = password[hash].empty();
return empty;
}
void insert(string user_name,string user_password){
int hash;
bool empty;
hash = hashfunc(user_name);
empty = is_slot_empty(hash);
//add an if condition to complete the code here
if(empty){
password[hash] = user_password;
cout<<"User added\n";
}
else{
cout<<"Hash slot is already taken\n";
}
}
void hash_lookup(string user_name){
int hash;
bool empty;
hash = hashfunc(user_name);
empty = is_slot_empty(hash);
//add an if condition to complete the code here
if(empty){
cout<<"User not found\n";
}
else{
cout<<"User password is "<<password[hash]<<"\n";
}
}
void delete_item(string user_name){
int hash;
bool empty;
hash = hashfunc(user_name);
empty = is_slot_empty(hash);
if(empty){
cout<<"No item found\n";
}
else{
password[hash].clear();
cout<<"User deleted\n";
}
}
void print_hashtable(){
for(int i=0;i<MAX_LENGTH;i++){
cout<<"["<<i<<"]-->"<<password[i]<<"\n";
}
}
};
int main(){
HashTable * hashtbl = new HashTable;
hashtbl->intialize_hashtable();
cout<< hashtbl->isFull()<<"\n";
int command=0;
string user_name;
string password;
while (command!=-1){
/* code */
cout << "Type command: ";
cin >> command;
switch (command){
case 1:
/* insert the new item*/
cout << "Enter user name: ";
cin >> user_name;
cout << "Enter password to be saved: ";
cin >> password;
hashtbl->insert(user_name,password);
break;
case 2:
/* delete item */
cout << "Enter item to be deleted: ";
cin >> user_name;
hashtbl->delete_item(user_name);
break;
case 3:
/* hash lookup password*/
cout << "Enter user name to look up password:";
cin >> user_name;
hashtbl->hash_lookup(user_name);
break;
case 4:
hashtbl->print_hashtable();
break;
case -1:
/* hash lookup password*/
cout << "Exiting...\n";
break;
}
}
return 0;
}