-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps3.cpp
248 lines (199 loc) · 6.39 KB
/
maps3.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
// Entry class to hold the letter and its corresponding cryptogram character
template <typename K, typename V>
class Entry {
private:
K _key; // Original letter
V _value; // Corresponding cryptogram letter
public:
// Constructor
Entry(K key, V value) : _key(key), _value(value) {}
// Destructor
virtual ~Entry() {}
// Getters and Setters
K getK() const { return _key; }
V getV() const { return _value; }
void setK(K key) { _key = key; }
void setV(V value) { _value = value; }
};
// Node for Doubly Linked List
template <typename K, typename V>
class Node {
public:
Entry<K, V> entry;
Node* next;
Node* prev;
Node(const Entry<K, V>& entry) : entry(entry), next(nullptr), prev(nullptr) {}
};
// NodeCryptogram class that implements a doubly linked list using std::map
template <typename K, typename V>
class NodeCryptogram {
private:
int uniqueKeys; // Current number of unique keys
int size; // Total number of entries
Node<K, V>* header; // Head sentinel
Node<K, V>* trailer; // Tail sentinel
std::map<K, Node<K, V>*> cryptogramMap; // Map for letter lookups
public:
// Constructor
NodeCryptogram();
// Destructor
virtual ~NodeCryptogram();
// Add a new cryptogram entry
void put(const K& key, const V& value);
// Find and return a node by key
Node<K, V>* find(const K& key);
// Remove entry by key
void erase(const K& key);
// Print all cryptogram entries from the beginning
void printCryptogram();
// Print all cryptogram entries from the end
void printCryptogramReverse();
// Get the number of unique keys
int uniqueKeysCount() const;
// Get the total number of entries
int size() const;
bool empty() const;
};
template <typename K, typename V>
NodeCryptogram<K, V>::NodeCryptogram() : uniqueKeys(0), size(0), header(nullptr), trailer(nullptr) {
header = new Node<K, V>(Entry<K, V>("", "")); // Create head sentinel
trailer = new Node<K, V>(Entry<K, V>("", "")); // Create tail sentinel
header->next = trailer;
trailer->prev = header;
}
template <typename K, typename V>
NodeCryptogram<K, V>::~NodeCryptogram() {
Node<K, V>* current = header->next;
while (current != trailer) {
Node<K, V>* temp = current;
current = current->next;
delete temp;
}
delete header;
delete trailer;
}
template <typename K, typename V>
void NodeCryptogram<K, V>::put(const K& key, const V& value) {
if (cryptogramMap.find(key) == cryptogramMap.end()) {
Entry<K, V> newEntry(key, value);
Node<K, V>* newNode = new Node<K, V>(newEntry);
cryptogramMap[key] = newNode;
// Insert the node in the list
Node<K, V>* lastNode = trailer->prev;
lastNode->next = newNode;
newNode->prev = lastNode;
newNode->next = trailer;
trailer->prev = newNode;
uniqueKeys++;
}
}
template <typename K, typename V>
Node<K, V>* NodeCryptogram<K, V>::find(const K& key) {
return cryptogramMap[key];
}
template <typename K, typename V>
void NodeCryptogram<K, V>::erase(const K& key) {
if (cryptogramMap.find(key) != cryptogramMap.end()) {
Node<K, V>* nodeToRemove = cryptogramMap[key];
nodeToRemove->prev->next = nodeToRemove->next;
nodeToRemove->next->prev = nodeToRemove->prev;
delete nodeToRemove;
cryptogramMap.erase(key);
uniqueKeys--;
}
}
template <typename K, typename V>
void NodeCryptogram<K, V>::printCryptogram() {
Node<K, V>* current = header->next;
while (current != trailer) {
cout << current->entry.getK() << " -> " << current->entry.getV() << endl;
current = current->next;
}
}
template <typename K, typename V>
void NodeCryptogram<K, V>::printCryptogramReverse() {
Node<K, V>* current = trailer->prev;
while (current != header) {
cout << current->entry.getK() << " -> " << current->entry.getV() << endl;
current = current->prev;
}
}
template <typename K, typename V>
int NodeCryptogram<K, V>::uniqueKeysCount() const {
return uniqueKeys;
}
template <typename K, typename V>
int NodeCryptogram<K, V>::size() const {
return size;
}
template <typename K, typename V>
bool NodeCryptogram<K, V>::empty() const {
return size == 0;
}
// Cryptogram class to manage the cryptogram key
class Cryptogram {
private:
string name; // Name of the cryptogram
NodeCryptogram<char, char> cryptogram; // Internal cryptogram key storage
public:
// Constructor
Cryptogram(string name);
// Destructor
virtual ~Cryptogram();
// Add letter mapping
void add(Entry<char, char> entry);
// Delete letter mapping by original letter
void deleteMapping(char letter);
// Print the cryptogram key
void printCryptogram(bool fromStart = true);
// Get the number of unique letter mappings
int uniqueMappings() const;
bool empty() const;
};
Cryptogram::Cryptogram(string name) : name(name) {}
Cryptogram::~Cryptogram() {}
void Cryptogram::add(Entry<char, char> entry) {
cryptogram.put(entry.getK(), entry.getV());
}
void Cryptogram::deleteMapping(char letter) {
cryptogram.erase(letter);
}
void Cryptogram::printCryptogram(bool fromStart) {
if (fromStart) {
cryptogram.printCryptogram();
} else {
cryptogram.printCryptogramReverse();
}
}
int Cryptogram::uniqueMappings() const {
return cryptogram.uniqueKeysCount();
}
bool Cryptogram::empty() const {
return cryptogram.empty();
}
int main() {
// Create a cryptogram key
Cryptogram myCryptogram("My Cryptogram");
// Add some letter mappings (cryptogram key)
myCryptogram.add(Entry<char, char>('a', 'z'));
myCryptogram.add(Entry<char, char>('b', 'y'));
myCryptogram.add(Entry<char, char>('c', 'x'));
myCryptogram.add(Entry<char, char>('d', 'w'));
// Print the cryptogram key from the beginning
cout << "Cryptogram from start:" << endl;
myCryptogram.printCryptogram(true);
// Print the cryptogram key from the end
cout << "Cryptogram from end:" << endl;
myCryptogram.printCryptogram(false);
// Remove a letter mapping
myCryptogram.deleteMapping('b');
// Print the cryptogram key after deletion
cout << "After deleting 'b':" << endl;
myCryptogram.printCryptogram(true);
return 0;
}