-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapTemplate.cpp
199 lines (190 loc) · 4.95 KB
/
MapTemplate.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
#include <iostream>
using namespace std;
template <class Key, class Value>
class Node{
public:
Key key;
Value value;
Node* prev;
Node* next;
Node(){};
Node(int k, int v){
key = k;
value = v;
prev = nullptr;
next = nullptr;
}
};
template <class K, class V>
class Map{
private:
Node<K,V>* head = nullptr;
Node<K,V>* tail = nullptr;
int length = 0;
public:
Map(){
cout<<"Non argument constructor"<<endl;
}
Map (Map& m){
auto temp = m.head;
for (int i = 0; i < m.length; i++)
{
push_back(temp->key,temp->value);
temp = temp->next;
}
}
~Map(){
if(head != nullptr){
while(head!=tail){
head=head->next;
delete head->prev;
}
delete head;
}
}
void push_front(K key, V value ){
if(!find(key)){
auto temp = new Node<K,V>(key, value);
if(head == nullptr){
head = tail = temp;
}else{
temp ->next = head;
head-> prev =temp;
head = temp;
}
length++;
}
}
void push_back(K key, V value){
if(!find(key)){
auto temp = new Node<K,V>(key,value);
if(head == nullptr){
head = tail = temp;
}else{
tail-> next = temp;
temp -> prev =tail;
tail = temp;
}
length++;
}
}
void insert(K key,V value, int index){
if(!find(key)){
auto inserted = new Node<K,V>(key,value);
auto temp = head;
if(is_empty()){
head = tail =inserted;
}
if(index==0){
push_front(key,value);
}else if(index==length-1){
push_back(key,value);
}else{
for (int i = 0; i !=index; i++){
temp=temp->next;
}
temp->prev->next=inserted;
inserted->prev=temp->prev;
temp->prev=inserted;
inserted->next=temp;
}
}
}
V pop_back(){
V x = tail->value;
tail = tail->prev;
delete tail->next;
tail->next = nullptr;
length--;
return x;
}
V pop_front(){
V x = head->value;
head = head->next;
delete head->prev;
head->prev = nullptr;
length--;
return x;
}
V pop(int n){
if(is_empty()){
return -1;
}
if(n==0){
return pop_front();
}
if(n==length-1){
return pop_back();
}
auto temp = get_node(n);
V x = temp->value;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete temp;
--length;
return x;
}
void print(){
for(auto temp = head;temp != nullptr;temp = temp-> next){
cout<<temp->key<<":"<<temp->value<<" ";
}
}
bool find(K key){
auto node = head;
for(int i=0;i<length;i++){
if(node->key==key){
return true;
}
node = node->next;
}
return false;
}
auto get_node(int n){
auto node = head;
for(int i=0;i<n;i++){
node = node->next;
}
return node;
}
V operator[](K key){
auto node = head;
while(node->key!=key){
node = node->next;
}
return node->value;
}
bool is_empty(){
return !head;
}
int size(){
return length;
}
bool operator==(Map& second){
if(length != second.size()){
return false;
}
auto temp1 = head;
auto temp2 = second.head;
for (int i = 0; i < length-1; i++)
{
if(temp1->value!=temp2->value && temp1->key!=temp2->key){
return false;
}
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
Map& operator=(Map& ll){
auto temp = head;
for (int i = 0; i < length-1; i++)
{
temp = temp->next;
ll.push_back(temp->key,temp->value);
}
return *this;
}
};
int main(){
return 0;
}