This repository has been archived by the owner on May 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGraph.h
229 lines (179 loc) · 6.96 KB
/
CGraph.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//CGraph.h
//Class for graphs
#ifndef CGRAPH_H
#define CGRAPH_H
#include <iostream>
#include "CList.h"
#include "CFunctions.h"
#include "CColor.h"
template < typename T1 >
class CGraph
{
private:
T1 *m_cur; //Current element
CList<CGraph>* m_outgoingNodes; //List of all outgoing node
CList<CGraph>* m_incomingNodes; //List of all incoming nodes
unsigned int m_numNodes; //Number of Elements in List
public:
//Constructor
CGraph()
{
m_outgoingNodes = new CList<CGraph>; //Declare list of outgoing nodes
m_incomingNodes = new CList<CGraph>; //Declare list of incoming nodes
m_numNodes = 0;
}
//getCur: return current element
T1* getCur() {
return m_cur;
}
//getOutgoing: return list of outgoing nodes
CList<CGraph>* getOutgoing() {
return m_outgoingNodes;
}
//getIncoming: return list of incoming nodes
CList<CGraph>* getIncoming() {
return m_incomingNodes;
}
//getNUm: return the number of nodes
unsigned int getNum() {
return m_numNodes;
}
//set: set first element in graph
void set(T1* element) {
m_cur = element;
}
//add: add ew element in graph
void add(T1* element, CList<T1>* incomingElements)
{
//Increase number of nodes
m_numNodes++;
//Create new node
CGraph* node = new CGraph;
//Assign element and list of incoming nodes to new node
node->m_cur = element; //Assign element to new node
node->m_incomingNodes = transfer(incomingElements); //Transfer list of elements to list of nodes
node->m_numNodes = m_numNodes - 1;
//Add new node to list of outcoming nodes in list of incoming nodes
int numNodes = node->m_incomingNodes->getNumElements()-1;
int nodeCounter = 0; //Counter for number of incoming node (=0)
//Loop running through all incoming nodes
while(nodeCounter <= numNodes)
{
//Create foo node, representing the incoming node
CGraph *before = node->m_incomingNodes->iterateElements(nodeCounter);
before->m_outgoingNodes->add(node); //Call "add" for all coming elements
nodeCounter++; //Increase nodeCounter
}
}
//transfer: transfers list of elements to list of nodes
CList<CGraph>* transfer(CList<T1>* listElements)
{
//Create a foo list, representing the list for all list of nodes
CList<CGraph>* listNodes = new CList<CGraph>;
//Transfer list of elements to list of nodes
int numElements = listElements->getNumElements()-1;
int elementCounter = 0; //Counter for number of elements in the list
//Loop running through all nodes
while (elementCounter <= numElements)
{
//Create foo element, representing the current element
T1* fooElement = listElements->iterateElements(elementCounter);
listNodes->add(getNode(fooElement->getNum())); //Call "add" for all coming nodes
elementCounter++; //Increase elementCounter
}
//Return list of incoming nodes
return listNodes;
}
//Find a node by the name of its element
CGraph* getNode(char* chName)
{
CFunctions F; //Class containing various functions (here: compare-function)
//Check whether given name matches name of current element
if(F.compare(chName, m_cur->getName()) == true)
return this;
//If not, then go through all its outgoing nodes
int numNodes = m_outgoingNodes->getNumElements()-1;
int nodeCounter = 0; //Counter for number of nodes in the list
//Loop running through all outgoing nodes
while(nodeCounter <= numNodes)
{
//foo node, representing the current outgoing node (search)
CGraph* foo = m_outgoingNodes->iterateElements(nodeCounter)->getNode(chName);
//Check, whether node was found
if(foo != NULL)
return foo; //return node
nodeCounter++; //Increase nodeCounter
}
//If no node was found, then return NULL
return NULL;
}
//Find a node by the number of its element
CGraph* getNode(unsigned int num)
{
//Check whether given number matches number of current element
if(num == m_cur->getNum())
return this;
//If not, then go through all its outgoing nodes
int numNodes = m_outgoingNodes->getNumElements()-1;
int nodeCounter = 0; //Counter for number of nodes in the list
//Loop running through all outgoing nodes
while(nodeCounter <= numNodes)
{
//foo node, representing the current outgoing node (search)
CGraph* foo = m_outgoingNodes->iterateElements(nodeCounter)->getNode(num);
//Check, whether node was found
if(foo != NULL)
return foo; //return node
nodeCounter++; //Increase nodeCounter
}
//If no node was found, then return NULL
return NULL;
}
//replaceElement: replace on element by another
void replaceElement(unsigned int numOldElement, T1* newElement)
{
CGraph* oldNode = getNode(numOldElement);
if(oldNode)
oldNode->set(newElement);
}
//deleteNode: deletes the given node
void deleteNode(unsigned int numNode)
{
//Create foo node: use foo node as given node from now on
CGraph* nodeDelete = getNode(numNode);
//Check, whether given node exists
if(nodeDelete)
{
int numNodes = nodeDelete->m_incomingNodes->getNumElements()-1;
int nodeCounter = 0; //node counter (=0)
//Loop running through all incomming nodes (delete node out of list of outgoing nodes)
while(nodeCounter <= numNodes)
{
//Create foo node: use foo as current incoming node
CGraph* incomingNode = nodeDelete->m_incomingNodes->iterateElements(nodeCounter);
//Delete given node from list of outgoing nodes
if(incomingNode->m_outgoingNodes->getElement(nodeDelete->m_numNodes))
incomingNode->m_outgoingNodes->deleteNode(nodeDelete->m_numNodes);
nodeCounter++; //Increase counter
}
delete nodeDelete;
}
else
cout << "Fatal Error" << endl;
}
//Print graph:
void print()
{
//print name of element
cout << m_cur->getName() << endl;
int numNodes = m_outgoingNodes->getNumElements()-1;
int nodeCounter = 0;
//Loop running through all outgoing nodes
while(nodeCounter <= numNodes)
{
m_outgoingNodes->iterateElements(nodeCounter)->print();
nodeCounter++;
}
}
};
#endif