-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_map.cpp
executable file
·108 lines (102 loc) · 2.19 KB
/
custom_map.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
#include <bits/stdc++.h>
#define None NULL
template <typename K, typename V>
struct Pair
{
K key;
V value;
};
typedef Pair<int, std::string> KeyValue;
struct Node
{
KeyValue data;
Node *next;
};
class Map
{
public:
Node *head;
Map()
{
head = None;
}
void insert(int k, std::string v)
{
Node *newNode = new Node();
newNode->data.key = k;
newNode->data.value = v;
newNode->next = head;
head = newNode;
}
void printMap()
{
Node *temp = head;
std::cout << "(Key, Value)" << std::endl;
while (temp)
{
std::cout << "(" << temp->data.key << ", " << temp->data.value << ")" << std::endl;
temp = temp->next;
}
}
Node *search(int k)
{
Node *temp = head;
while (temp)
{
if (temp->data.key == k)
{
return temp;
}
temp = temp->next;
}
return None;
}
};
int main()
{
Map m;
int val;
while (true)
{
std::cout << "1. Insert in Map. " << std::endl;
std::cout << "2. Search by Key. " << std::endl;
std::cout << "3. Print Map. " << std::endl;
std::cout << "4. Clear Screen. " << std::endl;
std::cout << "Choose Your Option: ";
std::cin >> val;
if (val == 1)
{
int k;
std::string v;
std::cin >> k >> v;
m.insert(k, v);
}
else if (val == 2)
{
int k;
std::cin >> k;
Node *res = m.search(k);
if (res)
{
std::cout << "(" << res->data.key << ", " << res->data.value << ")" << std::endl;
}
else
{
std::cout << "No Value Found." << std::endl;
}
}
else if (val == 3)
{
m.printMap();
}
else if (val == 4)
{
system("clear");
}
else
{
break;
}
}
return 0;
}