-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass3.cpp
More file actions
80 lines (78 loc) · 2 KB
/
ass3.cpp
File metadata and controls
80 lines (78 loc) · 2 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
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
class Hash_Table{
public:
string arr[26];
Hash_Table() {
for(int i = 0; i < 26; i++){
arr[i] = "never_used";
}
};
int find(string key){
int len = key.length();
char value = key[len-1];
int index = (int)value-97;
int tempIndex = index;
while(tempIndex < 26){
if(arr[tempIndex] == "never_used"){
return -1;
}else if(arr[tempIndex] == key){
return tempIndex;
}else{
tempIndex++;
tempIndex = tempIndex % 26;
}
}
return -1;
}
void insert(string key){
int len = key.length();
char value = key[len-1];
int index = (int)value-97;
int tempIndex = index;
if(find(key) == -1){
while(tempIndex < 26){
if(arr[tempIndex] == "never_used" || arr[tempIndex] == "tomb_stone"){
arr[tempIndex] = key;
break;
}else if(!arr[tempIndex].empty()){
tempIndex++;
tempIndex = tempIndex % 26;
}
}
}
};
void remove(string key){
if(find(key) != -1){
arr[find(key)]="tomb_stone";
}
}
void print(){
for(string a : arr){
if(a!="tomb_stone" && a!="never_used"){
cout << a << " ";
}
}
}
};
int main()
{
vector<string> operationsSet;
string line;
getline(cin, line);
istringstream iss(line);
for (string d; iss >> d; operationsSet.push_back(d)) {};
Hash_Table hashTable;
for(string operation : operationsSet){
string key = operation.substr(1, operation.find(" "));
if(operation[0]=='A'){
hashTable.insert(key);
}else{
hashTable.remove(key);
}
}
hashTable.print();
}