-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.h
63 lines (52 loc) · 1.26 KB
/
node.h
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
#ifndef _node_
#define _node_
#include <string>
#include <list>
#include <map>
#include <iostream>
using namespace std;
template<class T>
class Node;
typedef Node<int> intNode;
typedef intNode* pIntNode;
typedef list<pIntNode> listpNodesInt;
template<class T>
class Node {
public:
Node(){}
Node(string name):name(name){}
Node(string name,T value):
name(name),value(value){}
void addNode(Node<T> *node);
string getName(){return name;}
list<Node<T>*> getListNodes(){return listNodes;}
Node<T>* getNeighbourName(string name);
T getValue(){return value;}
protected:
list<Node<T>*> listNodes; // list of neighbours
map<string,Node<T>*> mapNodes; // access via name to neighbours
string name;
T value;
};
template<typename U>
void Node<U>::addNode(Node<U> *node){
listNodes.push_back(node);
string name=node->getName();
this->mapNodes[name]=node;
}
template<typename U>
Node<U>* Node<U>::getNeighbourName(string name){
// Get neighbour by name
try{
if(mapNodes[name] != NULL)
return mapNodes[name];
else
throw "Error accessing map of nodes. "+name+" key was not found";
}
catch(string e)
{
cout<<e<<endl;
}
return NULL;
}
#endif