-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
158 lines (132 loc) · 3.51 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
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
/*
* File: Node.h
* Author: hugh
*
* Created on July 4, 2016, 7:32 AM
*/
#ifndef NODE_H
#define NODE_H
#include <set>
#include <list>
using namespace std;
template <class T> class LinkedList {
private:
T* n;
LinkedList* next;
LinkedList* prev;
int size;
public:
LinkedList()
{
this->n = NULL;
this->size = 0;
this->next = NULL;
this->prev = NULL;
}
LinkedList(T* n)
{
this->n = n;
this->size = 0;
this->next = NULL;
this->prev = NULL;
}
int getSize()
{
return this->size;
}
void insert(T* n)
{
this->size++;
//if we are NULL, assign it to the current node
if (this->n == NULL)
{
this->n = n;
return;
}
//create a new node altogether
if (this->next == NULL)
{
this->next = new LinkedList<T>(n);
this->next->prev = this;
this->prev = NULL;
} else
{
//we want to avoid orphaning the list
//first link newNode to this->next
this->next->prev = new LinkedList<T>(n);
this->next->prev->next = this->next;
//now link head to newNode
this->next->prev->prev = this;
this->next = this->next->prev; //final step which would
//otherwise orphan the list
this->prev = NULL;
}
}
LinkedList* nextNode()
{
return this->next;
}
LinkedList* prevNode()
{
return this->prev;
}
bool is_null()
{
return (this->n == NULL);
}
T* data()
{
return this->n;
}
~LinkedList()
{} //see ~Node
};
class Node {
private:
std::string name, path;
std::set<std::string> pathSet;
LinkedList<Node>* children;
public:
Node(std::string name, std::string path)
{
this->name = name;
this->path = path;
this->children = new LinkedList<Node>();
}
std::string getName()
{
return this->name;
}
std::string getPath()
{
return this->path;
}
LinkedList<Node>* getChildren()
{
return this->children;
}
void add(Node* node)
{
if (this->pathSet.find(node->path) != pathSet.end())
return;
this->pathSet.insert(node->path);
this->children->insert(node);
}
~Node()
{
if (this->children != NULL)
{
LinkedList<Node>* current = this->children;
LinkedList<Node>* next = NULL;
while( current != NULL ) {
next = current->nextNode();
delete current->data();
delete current;
current = next;
}
current = NULL;
}
this->pathSet.erase(this->pathSet.begin(), this->pathSet.end());
}
};
#endif /* NODE_H */