forked from Estom/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_map.cpp
More file actions
64 lines (57 loc) · 1.09 KB
/
my_map.cpp
File metadata and controls
64 lines (57 loc) · 1.09 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
#include<map>
#include<string>
#include<iostream>
using namespace std;
struct Node{
string name;
Node* next;
Node(string _name){
name = _name;
next=nullptr;
}
};
class MyMap{
public:
map<string,Node*> m;
void insert(string s,Node* node){
if(m.count(s)>0){
Node* temp= m[s];
while(temp->next!=nullptr)temp=temp->next;
temp->next = node;
}
else{
m[s] = node;
}
}
Node* find(string s){
if(m.count(s)>0){
return m[s];
}
else{
return nullptr;
}
}
};
int main(){
MyMap mm;
string zhangsan("yanfabu");
Node* nzhangsan=new Node("zhangsan");
string wangyi("yanfabu");
Node* nwangyi = new Node("wangyi");
string lisi("shichangbu");
Node* nlisi =new Node("lisi");
mm.insert(zhangsan,nzhangsan);
mm.insert(wangyi,nwangyi);
mm.insert(lisi,nlisi);
Node* node1 = mm.find("yanfabu");
while(node1!=nullptr){
cout<<node1->name;
node1=node1->next;
}
Node* node2 = mm.find("shichangbu");
while(node2!=nullptr){
cout<<node2->name;
node2=node2->next;
}
return 0;
}